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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools