Here you can find the source of doZip(Map
Parameter | Description |
---|---|
IOException | an exception |
Exception | an exception |
private static long doZip(Map<String, File> zipStructure, File zipFile) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Map; import java.util.TreeMap; import java.util.zip.Deflater; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { /**//from ww w . j a v a 2s .co m * Zip up a bunch of files. * * @throws IOException * @throws Exception */ private static long doZip(Map<String, File> zipStructure, File zipFile) throws IOException { // sort the files by key TreeMap<String, File> sortedStructure = new TreeMap<String, File>(); sortedStructure.putAll(zipStructure); // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFile)); out.setComment("created with my program"); out.setMethod(ZipOutputStream.DEFLATED);// DEFLATED or STORED out.setLevel(Deflater.DEFAULT_COMPRESSION);// Deflater.NO_COMPRESSION // Create a buffer for reading the files byte[] buf = new byte[1024 * 10]; // Compress the files for (String key : sortedStructure.keySet()) { FileInputStream in = new FileInputStream( sortedStructure.get(key)); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(key)); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); return zipFile.length(); } }