Java Zip and Unzip OverviewProvides classes for reading and writing the standard ZIP and GZIP file formats. Also includes classes for compressing and decompressing data using the DEFLATE compression algorithm, which is used by the ZIP and GZIP file formats. Additionally, there are utility classes for computing the CRC-32 and Adler-32 checksums of arbitrary input streams. - java.util.zip.ZipOutputStream: an output stream wrapper that knows how to write to a stream destination (usually a file) while applying ZIP compression.
- java.util.zip.ZipEntry: used to identify the location (path) in the zip file to which the compressed file will be written.
Example of creating a ZIP fileThe steps required to compress files into a ZIP archive are as follows:- Create a ZipOutputStream that references the destination (for example, it can reference a file by wrapping a java.io.FileOutputStream)
- Create a java.io.InputStream (or derivative) that references a source file to add to the ZIP
- Create a ZipEntry that represents the InputStream created in step 2
- Add the ZipEntry to the ZipOutputStream by calling its putNextEntry() method
- Copy data from the InputStream to the ZipOutputStream (use a byte array buffer to read from the InputStream and then write it out to the ZipOutputStream)
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipFile { public static void gzipFile(String from, String to) { try{ FileInputStream in = new FileInputStream(from); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to)); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead); in.close(); out.close(); } catch (IOException e) { } } public static void zipDirectory(String dir, String zipfile) { try { File d = new File(dir); if (!d.isDirectory()) throw new IllegalArgumentException("Error! Not a directory: " + dir); String[] entries = d.list(); byte[] buffer = new byte[4096]; int bytesRead; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); for (int i = 0; i < entries.length; i++) { File f = new File(d, entries[i]); if (f.isDirectory()) continue;//Ignore directory FileInputStream in = new FileInputStream(f); ZipEntry entry = new ZipEntry(f.getPath()); out.putNextEntry(entry); // Store entry while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead); in.close(); } out.close(); } catch (IOException e) { } } public static void main(String args[]) { String from = "."; File f = new File(from); boolean directory = f.isDirectory(); ZipFile.zipDirectory(from, from + ".zip"); ZipFile.gzipFile(from, from + ".gz"); } }
Example of retrieving a compressed file from a ZIP fileUnzip is just the opposite of compression - Create a java.util.zip.ZipInputStream that references the zipped source (for example, to read a file make the ZipInputStream wrap a java.io.FileInputStream)
- Iterate over all the ZipEntrys contained in the archive by calling the ZipInputStream's getNextEntry() method
- Create a java.io.OutputStream that references the destination (for example a FileOutputStream) that matches the path of the ZipEntry
- Copy data from the ZipInputStream to the OutputStream, until the stream terminates (this signals the end of a ZipEntry)
- Close the OutputStream
- Get the next ZipEntry
try { // Open the ZIP file String inFilename = "test.zip"; ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename)); // Get the first entry ZipEntry entry = in.getNextEntry(); // Open the output file String outFilename = "o"; OutputStream out = new FileOutputStream(outFilename); // Transfer bytes from the ZIP file to the output file byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Close the streams out.close(); in.close(); } catch (IOException e) { } |