Solve ZipinputStream can't correctly handle Chinese file name

zhaozj2021-02-16  166

We know that Java is based on Unicode for text, so if it is processed by zipinputstream and zipOutputStream, the Chinese file name or path is encountered, which is of course to process Luo in Unicode! However, the compression and decompression of the software now, such as Winzip, but does not support Unicode, it does not process the file with Unicode encoded. How can we make the compressive file that WinZip can handle? Then you have to start from the modified zipinputstream and zipoutputstream to the file name. We can modify the original code of ZipinputStream and ZipOutputStream from JDK's src.zip: 1. ZipoutputStream.java 1. From JDK's src.zip, get zipoutputstream.java original code, save the new file to C: / Java / Util / ZIP This folder is changed to CZIPOUTPUTSTREAM.JAVA. 2. Start modifying the original code, change the Class name to CZIPOutputStream 3. Construction must also change to CZIPOutputStream 4. Add MEMBER, this MEMBER record encoding mode private String Encoding = "UTF-8"; 5. Add a new construction formula (this formula allows the class construct new time, set the file name coded) public CZipOutputStream (OutputStream out, String encoding) {super (out, new Deflater (Deflater.DEFAULT_COMPRESSION, true)); usesDefaultDeflater = true; THIS.Encoding = Encoding;} 6. Find Byte [] namebytes = getutf8bytes (e.Name); (have two places), modify it as follows: Byte [] namebytes = null; try {in (this.encoding.touppercase (). EQUALS ("UTF-8")) NameBytes = getutf8bytes (E.NAME); else nameBytes = E.Name.GetBytes (this.encoding);} catch (exceptation bytee) {namebytes = getutf8bytes (e.name) } 7. Save the file in the C: / Java / Util / Zip this folder, remember to have this path structure, in order to put the czipoutputstream.class in the correct Package structure 2, ZipinputStream.java 1. JDK's src.zip acquires the original code of ZipinputStream.java, saving new files to C: / Java / Util / Zip This folder is changed to CZipinputStream.java.

2. Start modifying the original code, change the Class name to CZipinputStream 3. The construction must also change to CzipinputStream 4. Add MEMBER, this MEMBER record encoding method private String Encoding = "UTF-8"; 5. Add a new construction The formula is as follows (this creative can make this class when new, set the file name encoded) Public CzipinputStream (InputStream IN, String Encoding) {Super (New PushbackInputStream (in, 512), New INFLATER (TRUE), 512) Usesdefaultinflater = true; if (in == null) {throw new nullpointerException ("in is null");} this.encoding = encoding;} 6. Find zipentry E = Createzilentry (Getutf8String (B, 0, LEN); This line is changed to the following: ZIPENTRY E = NULL; try {if (this.encoding.touppercase (). Equals ("UTF-8")) E = Createzilentry (GetF8String (B, 0, LEN)); ELSE E = CREATEZIPENTRY (New String (B, 0, Len, this.Encoding);} catch (exception bytee) {E = CREATEZIPENTRY (GetF8String (B, 0, LEN));} 7. Store files in C: / Java / Util / Zip This data clip, please remember to have this path structure, in order to put the CZipinputStream.class in the correct Package structure Save the above two files, compile generate czipoutputstream.class and czipinputstream.class, use WinZip to open [java_home] /jre/lib/rt.jar This file, add czipoutputstream.class and czipinputstream.class, remember "s" AVE FULL PATH INFO must be tick. When compressed and decompressed, there is a Chinese file name and path, you can specify the encoding method to process. CZIPOUTPUTSTREAM ZOS = New CzipOutputStream (OutputStream OS, String Encoding);

CzipinputStream ZINS = New CzipinputStream (InputStream INS, STRING ENCODING);

"Compression and decompression (1)" as an example:

FileOutputStream Fos = New FileoutputStream (Request.GetRealPath ("/") "Myzip.zip");

CZIPOUTPUTSTREAM ZOS = New CzipOutputStream (FOS, "GBK");

If you don't have to change elsewhere, you can handle the compression of Chinese file names.

转载请注明原文地址:https://www.9cbs.com/read-8899.html

New Post(0)