I have a piece of code is compressed file into a zip format, but encountered the problem is that the folder contains Chinese, can not be opened after compression, the hint "no file is decompressed"; query data to draw conclusions, JDK native Zipoutputstream support for Chinese is not very good, It is recommended to use Ant's Zipoutputstream; download Ant.jar, there is this class, do not need to change the code, the jar package into the project, as the class name, the Java file in the original package name to change the line; Modified code:
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipOutputStream;
Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipOutputStream; public class Filezip {/** * @param inputfilename * You want to compress the folder (entire full path) * @param zipfilename * Compressed file (entire full path) */public static void Zip (String inputfilename, String zipfilename) throws Exception {Zip (zipfil
ename, New File (InputFileName)); private static void Zip (String zipfilename, File inputfile) throws Exception {Zipoutputstream out = new ZIPOUTPU
Tstream (New FileOutputStream (Zipfilename));
Out.setencoding (System.getproperty ("sun.jnu.encoding"));
Zip (out, Inputfile, "");
Out.close (); private static void Zip (Zipoutputstream out, File F, String Base) throws Exception {if (F.isdirectory ()) {Fi
le[] fl = F.listfiles (); Base = base.length () = = 0? "": Base + "/"; Note that the left slant hereBar Out.putnextentry (new ZipEntry (base));
for (int i = 0; i < fl.length i++) {zip (out, fl[i], base + fl[i].getname ());
} else {out.putnextentry (new ZipEntry (base));
FileInputStream in = new FileInputStream (f);
byte[] buffer = new byte[1024];
int n = in.read (buffer);
while (n!=-1) {out.write (buffer, 0, N);
n = in.read (buffer);
} in.close ();
} public static void Main (string[] temp) {try {zip ("d:\\servers\\apache-tomcat-7.0.59\\webapps\\aaa\\admin"),
"E:\\test.zip");
catch (Exception ex) {ex.printstacktrace (); }
}
}
In particular, you need the following code, or you will complain
Out.setencoding (System.getproperty ("sun.jnu.encoding"));