Java compression unzip zip and solve Linux in Chinese garbled

Source: Internet
Author: User

1. [Code][java] Code
1: Before compressing, to set up Linux mode, need to use third party Ant-1.6.5.jar
If it is a file directory, the
ZipEntry zipentry=new zipentry (BasePath + system.getproperties (). GetProperty ("File.separator"));
Zipentry.setunixmode (755);//Solve Linux garbled

If it is a file, the
ZipEntry zipentry=new ZipEntry (base);
Zipentry.setunixmode (644);//Solve Linux garbled

Then, force the encoding at output:
Zipoutputstream out = new Zipoutputstream (New FileOutputStream (
Zipfilename));
Out.setencoding ("GBK");//Solve Linux garbled

The full code is as follows:


Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.OutputStream;

Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipOutputStream;

Import Org.apache.tools.ant.Project;

Import Org.apache.tools.ant.taskdefs.Expand;

/**
* Folder compression, support for Win and Linux
* @author Wlzhang
*
*/
public class Ziputil
{
/**
* @param inputfilename
* Enter a folder
* @param zipfilename
* Output a compressed folder, after packaging the file name
* @throws Exception
*/
public static OutputStream Zip (string inputfilename, String zipfilename) throws Exception
{
return Zip (zipfilename, new File (InputFileName));
}

private static OutputStream Zip (String zipfilename, File inputfile) throws Exception
{
Zipoutputstream out = new Zipoutputstream (New FileOutputStream (
Zipfilename));
Out.setencoding ("UTF-8");//solve Linux garbled according to the actual encoding settings of the Linux system, you can use the command vi/etc/sysconfig/i18n to view the Linux system encoding
Zip (out, Inputfile, "");
Out.close ();
return out;
}

private static void Zip (Zipoutputstream out, File F, String base) throws Exception
{http://www.huiyi8.com/bgm/background music?
if (F.isdirectory ())
{//Determine if the directory
file[] fl = F.listfiles ();
Out.putnextentry (new Org.apache.tools.zip.ZipEntry (base + "/"));
Out.putnextentry (new ZipEntry (base + "/"));
ZipEntry zipentry=new ZipEntry (base + system.getproperties (). GetProperty ("File.separator"));
Zipentry.setunixmode (755);//Solve Linux garbled
Out.putnextentry (ZipEntry);
Base = base.length () = = 0? "": Base + "/";
Base = base.length () = = 0? "": Base + system.getproperties (). GetProperty ("File.separator");
for (int i = 0; i < fl.length; i++)
{
Zip (out, fl[i], base + fl[i].getname ());
}
}
Else
{//Compress all files in the directory
Out.putnextentry (new Org.apache.tools.zip.ZipEntry (base));
ZipEntry zipentry=new ZipEntry (base);
Zipentry.setunixmode (644);//Solve Linux garbled
Out.putnextentry (ZipEntry);
FileInputStream in = new FileInputStream (f);
int b;
while ((b = In.read ())! =-1)
{
Out.write (b);
}
In.close ();
}
}

private static void Unzip (String sourcezip,string DestDir) throws exception{

try{

Project P = new Project ();

Expand e = new Expand ();

E.setproject (P);

E.SETSRC (New File (Sourcezip));

E.setoverwrite (FALSE);

E.setdest (New File (DestDir));

/*

The Zip tool under ant has a default compression encoding of UTF-8 encoding,

While WinRAR software compression is used by Windows default GBK or GB2312 encoding

Therefore, the encoding format should be developed when extracting

*/

E.setencoding ("UTF-8"); Based on the actual encoding settings of the Linux system

E.execute ();

}catch (Exception e) {

Throw e;

}

}


}

Java compression unzip zip and solve Linux in Chinese garbled

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.