Java compressed file/directory into Zip package latest tips

Source: Internet
Author: User
Tags rar zip

Using Java's own class java.util.zip for file/directory compression, there is a lack of support for Chinese name/directory naming, if there is a Chinese name, then packaging will fail. I after a period of exploration and practice, found in the General Ant.jar Class Library also has a zip class, import org.apache.tools.zip.*, can solve the problem that does not support the Chinese file directory, at the same time, ant access to more ways, The general application server has this package, it is really not to download a tomcat5. X, there are also Ant.jar, I have been tested, you can use.

Examples are as follows:

import java.io.File;
import org.apache.tools.zip.ZipOutputStream;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import dianda.cwmanage.*;
Import Dianda.com.util.Format;
public class Compressbook {
Public compressbook () {
}
public void Zip (String inputfilename) throws Excep  tion {
String zipfilename= "c:\\test.zip";//packaged file name
System.out.println (zipfilename);
Zip (zipfilename, new File (InputFileName));
}
private void Zip (String zipfilename, File inputfile) throws Exception {
Zipoutputstream out = new ZIPOUTPU Tstream (New FileOutputStream (Zipfilename));
Zip (out, Inputfile, "");
System.out.println ("Zip done");
Out.close ();   
}
private void Zip (Zipoutputstream out, File F, String Base) throws Exception {
if (f.isdirectory ()) {
file[] fl = F.listfiles ();
Out.putnextentry (new Org.apache.tools.zip.ZipEntry (base + "/"));
Base = base.length () = 0? "": Base + "/";
for (int i = 0; i < fl.length i++) {
Zip (out, fl[i], base + fl[i].getname ());
  
}
Else {
Out.putnextentry (new Org.apache.tools.zip.ZipEntry (base));
FileInputStream in = new FileInputStream (f);
Int b;
System.out.println (base);   
while ((b = in.read ())!=-1) {
Out.write (b);
}
In.close ();
}
}
}

The only regret, in the ointment, is that, whatever, Java.util.zip or org.apache.tools.zip can not extract RAR tools into the RAR bag, search for a half-day also can not find the use of Java Decompression RAR tool into the bag, hoping which person can provide decompression RAR tool into the package, and you share!

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.