Java compressed zip file Chinese garbled problem solving method _java

Source: Internet
Author: User

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 (); }
}

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.