package com.mindprod.example;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import static java.lang.System.*;
/**
* Example to read the elements of a Zip file randomly by name lookup.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2008-06-06
* @since 2008-06-06
*/
public final class TestZipReadRandom
{
/**
* 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 elementName = "com/mindprod/batik/run.bat";
final String targetdir = "targetdir";
final ZipFile zip = new ZipFile( zipFileName );
final ZipEntry entry = zip.getEntry( elementName );
final int fileLength = ( int ) entry.getSize();
final byte[] wholeFile = new byte[ fileLength ];
final InputStream is = zip.getInputStream( entry );
final int bytesRead = is.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.close();
}
}