package com.mindprod.example;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import static java.lang.System.*;
/**
* Example to read the elements of a Zip file sequentially.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2008-06-06
* @since 2008-06-06
*/
public final class TestZipRead
{
/**
* read the elements of a Zip file sequentially.
*
* @param args not used
*
* @throws java.io.IOException if problems writing file a.zip
*/
public static void main( String[] args ) throws IOException
{
final String zipFileName = "in.zip";
final String targetdir = "targetdir";
final FileInputStream fis = new FileInputStream( zipFileName );
final ZipInputStream zip = new ZipInputStream( fis );
while ( true )
{
final ZipEntry entry = zip.getNextEntry();
if ( entry == null )
{
break;
}
final String elementName = entry.getName();
final int fileLength = ( int ) entry.getSize();
final byte[] wholeFile = new byte[ fileLength ];
final int bytesRead = zip.read( wholeFile, 0, fileLength );
out.println( bytesRead + " bytes read from " + elementName );
final File elementFile = new File( targetdir, elementName );
elementFile.getParentFile().mkdirs();
final FileOutputStream fos = new FileOutputStream( elementFile );
fos.write( wholeFile, 0, fileLength );
fos.close();
elementFile.setLastModified( entry.getTime() );
zip.closeEntry();
}
zip.close();
}
}