Home  >  Article  >  Java  >  Solution to zip compression garbled characters in java (with code)

Solution to zip compression garbled characters in java (with code)

尚
Original
2019-11-26 10:52:132609browse

Solution to zip compression garbled characters in java (with code)

Use java to package files to generate compressed files. There are two places where garbled characters will appear (recommended: java basic tutorial)

1. Content Many people on the Internet have given solutions to the Chinese garbled problem. There are two solutions: modify the source code of sun; use the open source class libraries org.apache.tools.zip.ZipOutputStream and org.apache.tools.zip.ZipEntry. There are two classes in ant.jar, which can be downloaded and used. There is no doubt that it is more convenient to choose the latter.

2. The problem of Chinese garbled characters in compressed file comments: zos.setComment("Chinese test"); I checked this problem online for a long time and didn't see anyone explaining it, so I had to find a way to solve it myself.

There are no problems with the test classes created by the project on my own machine, but when used in the company's projects, garbled characters always appear. By using the method of setting the encoding (zos.setEncoding("gbk");), I finally The problem was discovered. The encoding method of the test project is gbk, and the default encoding of the company's project is utf-8, so there is no problem with the test project but there is a problem with the company's project.

org.apache.tools.zip.ZipOutputStream uses the encoding method of the project by default. Theoretically speaking, utf-8 also supports Chinese. If you can’t figure out why it is still garbled, just change it to gbk through the setEncoding method. solve.

Attach a code for compressing the file

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) throws Exception {  
        File f = new File("中文测试.txt");  
        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(  
                new FileOutputStream("zipTest.zip"), 1024));  
        zos.putNextEntry(new ZipEntry("中国人.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("中文测试");  
  
        zos.closeEntry();  
        zos.close();  
    }  
  
}

The above is the detailed content of Solution to zip compression garbled characters in java (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn