Java Compression and decompression __java

Source: Internet
Author: User
Tags getmessage

Package com.common.util;

Import java.io.*;
Import java.util.ArrayList;
Import java.util.zip.*;

public class UnZip {

public static int icompresslevel; The range of compression ratio is 0~9

public static Boolean boverwrite; Whether to overwrite file with same name range is true and false

private static ArrayList Allfiles = new ArrayList ();

public static String Serrormessage;

public static ArrayList UnZip (String zippathfile, String destpath) {
ArrayList allfilename = new ArrayList ();
try {
Specify the location and filename of the compressed file, and create the FileInputStream object
FileInputStream in = new FileInputStream (zippathfile);
To pass fins into the Zipinputstream
Zipinputstream Zin = new Zipinputstream (in);
ZipEntry ent = null;
byte ch[] = new byte[256];
while ((ent = Zin.getnextentry ())!= null) {
File Zfile = new file (DestPath + ent.getname ());
File Fpath = new file (Zfile.getparentfile (). GetPath ());
if (Ent.isdirectory ()) {
if (!zfile.exists ())
Zfile.mkdirs ();
Zin.closeentry ();
} else {
if (!fpath.exists ())
Fpath.mkdirs ();
FileOutputStream Fouts = new FileOutputStream (zfile);
int i;
Allfilename.add (Zfile.getabsolutepath ());
while ((i = Zin.read (ch))!=-1)
Fouts.write (CH, 0, I);
Zin.closeentry ();
Fouts.close ();
}
System.out.println ("Extract file:" + ent.getname () + zippathfile);
}
In.close ();
Zin.close ();
Serrormessage = "OK";
catch (Exception e) {
SYSTEM.ERR.PRINTLN ("Extract error:" + e.getmessage ());
Serrormessage = E.getmessage ();
}
Allfiles.clear ();
System.out.println ("complete");
return allfilename;
}

public static void Main (string[] args) {

Unzip.unzip ("C://order_20.rar", "c://un//");

}
}

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.