Zip compression and decompression in Java-garbled Chinese file names Solution

Source: Internet
Author: User

Apache ant has a package dedicated to processing ZIP files. You can specify the file name encoding method. This can solve the problem.

For example, use org.apache.tools.zip.zipoutputstreamto replace java.util.zip. zipoutputstream.

Java uses Unicode as the basis for text encoding. Therefore, if zipinputstream and zipoutputstream are used to compress and decompress data, it must be UNICODE when it comes to the Chinese document name or path. However, the compression and decompression software on the market, such as WinZip, does not support Unicode. Once a file name is encoded in UNICODE, it will not be processed. So how can we make the compression files that can be processed by WinRAR? Then we have to start by modifying the zipinputstream and zipoutputstream encoding method for the file name. We can get the original zipinputstream and zipoutputstream codes from src.zip of JDK to modify them.

1. zipoutputstream. Java

1. Get the original zipoutputstream. Java code from src.zip of JDK and save it to a new file. Change the file name to cnzipoutputstream. java.
2. modify the original code and change the class name to cnzipoutputstream.
3. the constructor must also be changed to cnzipoutputstream.
4. added the member record encoding method.
Private string encoding = "UTF-8 ";
5. Add another constructor (this constructor allows this class to set the file name encoding when it is new)
Public czipoutputstream (outputstream out, string encoding ){
This (out );
This. Encoding = encoding;
}
6. Find byte [] namebytes = getutf8bytes (E. Name); (there are two places) and modify it as follows:
Byte [] namebytes = NULL;
Try
{
If (this. encoding. touppercase (). Equals ("UTF-8 "))
Namebytes = getutf8bytes (E. Name );
Else
Namebytes = E. Name. getbytes (this. Encoding );
}
Catch (exception bytee)
{
Namebytes = getutf8bytes (E. Name );
}

Ii. zipinputstream. Java
1. Get the original zipinputstream. Java code from src.zip of JDK and save it to a new file. Change the file name to cnzipinputstream. java.
2. modify the original code and change the class name to cnzipinputstream.
3. the constructor must also be changed to cnzipinputstream.
4. added the member record encoding method.
Private string encoding = "UTF-8 ";
5. Add another constructor as follows (this constructor allows this class to set the file name encoding when it is new)
Public czipinputstream (inputstream in, string encoding ){
This (in );
This. Encoding = encoding;
}
6. Find zipentry E = createzipentry (getutf8string (B, 0, Len); and change it to the following:
Zipentry E = NULL;
Try
{
If (this. encoding. touppercase (). Equals ("UTF-8 "))
E = createzipentry (getutf8string (B, 0, Len ));
Else
E = createzipentry (new string (B, 0, Len, this. Encoding ));
}
Catch (exception bytee)
{
E = createzipentry (getutf8string (B, 0, Len ));
}

After the above two files are stored, compile generates czipoutputstream. class and czipinputstream. class, use WinZip to enable [java_home] \ JRE \ Lib \ RT. JAR file. class and cnzipinputstream. after adding the class, you can specify the encoding method when there is a problem with the document name and path during compression and decompression.
Usage:
Cnzipoutputstream ZOS = new cnzipoutputstream (outputstream OS, string encoding );
Cnzipinputstream Zins = new cnzipinputstream (inputstream INS, string encoding );
The solution to the problem of Chinese files encountered during file packaging and downloading in the previous article is:
Outputstream OS = response. getoutputstream ();
Cnzipoutputstream ZOS = new cnzipoutputstream (OS, "GBK"); // Add the Chinese encoding type

Related Article

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.