Use Zipoutputstream to compress files, report no files to be decompressed problem

Source: Internet
Author: User
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"));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.