Usually in Java to pack files to generate compressed files, the following two places will appear garbled:
1, the content of Chinese garbled problem, the problem on the internet a lot of people have given a solution, there are two main ways: first, to modify the sun's source code The other is the use of Open source class library Org.apache.tools.zip.ZipOutputStream and Org.apache.tools.zip.ZipEntry, which are available in the two classes Ant.jar, can be downloaded directly, no doubt, it is more convenient to choose the latter
2, compressed file comments in Chinese garbled problem: zos.setcomment ("Chinese test"); The problem is that there is less information on the online solution. The test class created on your own machine does not have any problems, but in the company's project use has been garbled, by using the method of setting encoding (zos.setencoding ("GBK"), finally found the problem, the test project is encoded in GBK, The default encoding for a company project is Utf-8, so there is no problem with the test project and the company's project.
Org.apache.tools.zip.ZipOutputStream default use of the project encoding way, theoretically speaking utf-8 is also supporting Chinese, really think impassability why or garbled, through the Setencoding method to GBK can be resolved
The sample code for the above problem is as follows:
package com.compress;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.DataInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipOutputStream; public class Compressencodingtest {/** * @param args * @throws Exception/public static void Main (string[) args) t
Hrows Exception {File F = new file ("Chinese test. txt");
Zipoutputstream Zos = new Zipoutputstream (New Bufferedoutputstream ("FileOutputStream"), 1024);
Zos.putnextentry (New ZipEntry ("Chinese. txt"));
DataInputStream dis = new DataInputStream (new Bufferedinputstream (new FileInputStream (f)));
Zos.putnextentry (New ZipEntry (F.getname ()));
int C;
while ((c = dis.read ())!=-1) {zos.write (c);
} zos.setencoding ("GBK");
Zos.setcomment ("Chinese test");
Zos.closeentry ();
Zos.close (); }
}