注:java.util.zip.ZipOutputStream是Java原生类,在JDK1.7之后构造器加入了编码方式,已经可以支持中文。
ZipOutputStream public ZipOutputStream(OutputStream out, Charset charset) Creates a new ZIP output stream. Parameters:out - the actual output streamcharset - the charsetto be used to encode the entry names and comments Since:1.7
package test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.Adler32; import java.util.zip.CheckedInputStream; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class ZipCompress { private static final String FILE_PREFIX_PATH = "E:\\gittest\"; private static final String FILE_TARGET_PATH = "E:\\gittest\Test.zip"; private static List<String> fileList = new ArrayList<String>() { { add("第三方的身份.txt"); add("Test2.txt"); add("Test3.txt"); } }; public static void main(String[] args) { try { FileOutputStream f = new FileOutputStream(FILE_TARGET_PATH); CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32()); ZipOutputStream zos = new ZipOutputStream(csum, Charset.forName("UTF-8")); PrintWriter out = new PrintWriter(zos); zos.setComment("A test of java Zipping"); // No corresponding getComment(), though. for (String item : fileList) { System.out.println("Writing file: " + item); BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(FILE_PREFIX_PATH + item), "UTF-8")); zos.putNextEntry(new ZipEntry(item)); String c; while ((c = in.readLine()) != null) { out.println(c); } in.close(); out.flush(); } out.close(); // Checksum valid only after the file has been closed! System.out.println("Checksum: " + csum.getChecksum().getValue()); // Now extract the files - Example1: System.out.println("========================Reading File - Example 1================================"); CheckedInputStream csum2 = new CheckedInputStream(new FileInputStream(FILE_TARGET_PATH), new Adler32()); ZipInputStream in2 = new ZipInputStream(csum2, Charset.forName("UTF-8")); BufferedReader bi2 = new BufferedReader(new InputStreamReader(in2)); ZipEntry ze; while ((ze = in2.getNextEntry()) != null) { System.out.println("Reading file: " + ze); String x; while ((x = bi2.readLine()) != null) { System.out.println(x); } // Line feed System.out.println(); } if (fileList.size() == 1) { System.out.println("Checksum: " + csum.getChecksum().getValue()); } bi2.close(); // Now extract the files - Example2: System.out.println("========================Reading File - Example 2================================"); ZipFile zf = new ZipFile(FILE_TARGET_PATH); Enumeration e = zf.entries(); while (e.hasMoreElements()) { ZipEntry ze2 = (ZipEntry) e.nextElement(); System.out.println("File: " + ze2); BufferedReader bi3 = new BufferedReader(new InputStreamReader(zf.getInputStream(ze2))); String x; while ((x = bi3.readLine()) != null) { System.out.println(x); } // Line feed System.out.println(); bi3.close(); } if (fileList.size() == 1) { System.out.println("Checksum: " + csum.getChecksum().getValue()); } } catch (IOException e) { throw new RuntimeException(e); } } }
运行: