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.ZipOutputStream;
import static java.lang.System.*;
/**
* Example to create a Zip file with one element.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2008-06-06
* @since 2008-06-06
*/
public final class TestZipWrite
{
/**
* create a Zip file with one element.
*
* @param args not used
*
* @throws java.io.IOException if problems writing file out.zip
*/
@SuppressWarnings( { "UnusedAssignment" } )
public static void main( String[] args ) throws IOException
{
final String zipFilename = "out.zip";
final String elementName = "mydir/test.txt";
final FileOutputStream fos = new FileOutputStream( zipFilename );
final ZipOutputStream zip = new ZipOutputStream( fos );
zip.setLevel( 9 );
zip.setMethod( ZipOutputStream.DEFLATED );
final File elementFile = new File( elementName );
final ZipEntry entry = new ZipEntry( elementName );
entry.setTime( elementFile.lastModified() );
final int fileLength = ( int ) elementFile.length();
final FileInputStream fis = new FileInputStream( elementFile );
final byte[] wholeFile = new byte[ fileLength ];
final int bytesRead = fis.read( wholeFile, 0, fileLength );
out.println( bytesRead + " bytes read from " + elementName );
fis.close();
zip.putNextEntry( entry );
zip.write( wholeFile, 0, fileLength );
zip.closeEntry();
zip.close();
}
}