The file name in Windows is normal, but the Chinese file name is garbled after zipoutputstream is compressed in Linux.

Source: Internet
Author: User
The file name in Windows is normal, but the file name in Linux is garbled.
After one night, I knew it was a coding problem. I found two solutions on the Internet (none of them seem to work ):
1. Modify the corresponding class in java.util.zip. It is too troublesome to change this method, and the ant package already has a ready-made method, and it is useless (maybe I didn't change it)
2. Replace the java.util.zipin import with org.apache.tools.zip provided by ant. This method supports Chinese Characters in windows, but garbled characters are still displayed in Linux.

Today, let's look back and add a setencoding ("GBK") to the zipoutputstream object in the case of the second method. Write down your understanding so that you can view similar problems. If you have any questions, please point them out.
Solution:
Cause: org.apache.tools.zip. zipoutputstream uses the system encoding by default (windows are GBK, Linux is UTF-8), and GBK is used for decompression in windows, because some files are compressed in Linux, unzipping in the window will cause garbled characters, because some of them will be encoded as GBK during compression.
The Code is as follows:
Import org.apache.tools.zip. zipentry;
Import org.apache.tools.zip. zipoutputstream;

/**
* How to compress ZIP files/directories with multiple specified files.
*
* @ Param inputfilenames
* File name/directory to be added to the compressed file
* @ Param outputfilename
* File Name of the compressed file
*/
Public static Boolean zipfiles (list inputfilenames, string outputfilename ){
Boolean result = true;
Try {
Zipoutputstream out = NULL;
Try {
File parent = new file (outputfilename). getparent (); // get the parent folder
If (! Parent. exists ()){
Parent. mkdirs ();
}
Out = new zipoutputstream (New fileoutputstream (outputfilename ));
Out. setencoding ("GBK"); // specify the Encoding As GBK. Otherwise, garbled characters will occur when deployed in Linux.
File inputfile = NULL;
For (Object OBJ: inputfilenames ){
Object [] input = (object []) OBJ;
If (input. length! = 2)
Continue;
Inputfile = new file (string) input [0]);
String filen = (string) input [1];
Zip (Out, inputfile, filen );
}
System. Out. println ("Zip done ");
} Catch (exception e ){
Result = false;
} Finally {
If (OUT! = NULL)
Out. Close ();
}
} Catch (exception e ){
Result = false;
E. printstacktrace ();
}
Return result;
}

Private Static Boolean zip (zipoutputstream out, file, string base ){
Boolean result = true;
Try {
If (file. isdirectory ()){
File [] subfiles = file. listfiles ();
If (base. Length ()! = 0 ){
Out. putnextentry (New zipentry (base + file. separator ));
Base + = file. Separator;
}

For (INT I = 0; I <subfiles. length; I ++ ){
Zip (Out, subfiles [I], base + subfiles [I]. getname ());
}
} Else {
String filename = base; // java.net. urlencoder. encode (base, "UTF-8 ");
Out. putnextentry (New zipentry (filename ));
Fileinputstream in = new fileinputstream (File );
Int B;
System. Out. println (filename );
Try {
While (B = in. Read ())! =-1 ){
Out. Write (B );
}
} Catch (exception e ){
Result = false;
} Finally {
In. Close ();
}
}
} Catch (exception e ){
Result = false;
}
Return result;
}

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.