Unzip a zip package with Chinese name files

Source: Internet
Author: User
Tags rar

When Java.util.zip.ZipInputStream and Java.util.zip.ZipOutputStream compress and decompress a zip package with a Chinese name file, the following exception is reported:

Java.lang.IllegalArgumentException
At Java.util.zip.ZipInputStream.getUTF8String (zipinputstream.java:293)
At Java.util.zip.ZipInputStream.readLOC (zipinputstream.java:247)
At Java.util.zip.ZipInputStream.getNextEntry (zipinputstream.java:74)
At Com.test.ZipTest.unZipAssetSouce (ziptest.java:25)
At Com.test.ZipTest.main (ziptest.java:14)

The reason is that the JDK comes with the zip utility class, the default read file name is UTF-8 encoding, and for those files with a UTF-8 encoded file will not be properly read, such as the use of WinZip compressed ZIP package will not be able to process a file with Chinese names, it uses ASCII-encoded, This problem is often encountered in development, and the following solutions are given:

Extracts several processing zip-related classes within the JDK, Deflater.java,deflateroutputstream.java,inflaterinputstream.java,zipconstants.java, Zipentry.java,zipinputstram.java, and then customize a Compresszzipfile.java read into the zip file, source code:

Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;

Import Java.io.FileOutputStream;
	public class Compresszzipfile {static final int BUFFER = 2048; /** * Read zip file, only suitable for the zip file is not valid for RAR files, because the zip file compression algorithm is public, and RAR is not * @version 1.0 * @param zipfilepath:zip file path, Unzippath: to extract to
			File path */public void Readzip (String zipfilepath,string unzippath) {try {bufferedoutputstream bos = null;
			Create input byte stream FileInputStream fis = new FileInputStream (Zipfilepath);
			Create an input character stream based on the input streams Bufferedinputstream bis = new Bufferedinputstream (FIS);
			Creates a zip file input stream Zipinputstream ZiS = new Zipinputstream (bis), based on character streams;
			ZIP file entry, representing the zip file ZipEntry entry;
				Iterates through the file entries, as long as they are not empty, while ((Entry=zis.getnextentry ())!=null) {System.out.println () ("= =" +entry.getname ());
				int count;
				byte date[] = new Byte[buffer];
				If the entry is a file directory, continue to execute if (entry.isdirectory ()) {continue; }else{int begin = ZipFilePath.lastindexof ("\") +1; int end = Zipfilepath.lastindexof (".")
					+1;
					String ziprealname = zipfilepath.substring (begin,end); BOS = new Bufferedoutputstream (new FileOutputStream (this.getrealfilename unzippath+ "\" +ziprealname,entry.getname (
					))));
					while ((Count=zis.read (date))!=-1) {bos.write (date,0,count);
				} bos.flush (); Bos.close ();
		} zis.close ();
		catch (Exception e) {e.printstacktrace (); } private File Getrealfilename (String zippath,string absfilename) {string[] dirs = Absfilename.split ("/", Absfilena
		Me.length ());
		Creates a file object, filename = new file (Zippath);  if (dirs.length>1) {for (int i=0;i<dirs.length-1;i++) {/////Create a new file object based on the file abstract path and dir path string, the path is the previous directory files =
			New File (File,dirs[i]);
		} if (!file.exists ()) {file.mkdirs ();
		File = new file (file,dirs[dirs.length-1]);
	return file; }
}

Then build a test class Ziptest.java, source code:

Import Com.zip.tool.CompresszZipFile;

public class Ziptest {public

	static void Main (String args[]) {
		
		Compresszzipfile compress = new Compresszzipfile () ;
		Compress. Readzip ("D:\\android\\workspace\\test\\word.zip", "d:\\test");
	}

Test run OK ...

To facilitate testing, the above related classes into a jar package to share to everyone. (Some of the above source sources from the network)

http://download.csdn.net/download/adreamer_bj/3745253

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.