Java Decompression zip archive package

Source: Internet
Author: User

Package com.spring.mvc.zip;

Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.Enumeration;
Import Java.util.zip.ZipEntry;

Import Org.apache.tools.zip.ZipFile;


public class Zip {

/**
* Unzip to the specified directory
* @param Zippath
* @param descdir
* @author isea533
*/
public static void Unzipfiles (String zippath,string descdir) throws exception{
Unzipfiles (New File (Zippath), descdir);
}
/**
* Extract files to the specified directory
* @param zipfile
* @param descdir
* @author isea533
*/
@SuppressWarnings ("Rawtypes")
public static void Unzipfiles (File zipfile,string descdir) throws exception{
File Pathfile = new file (Descdir);
if (!pathfile.exists ()) {
Pathfile.mkdirs ();
}
ZipFile zip = new ZipFile (ZipFile, "GBK");
For (Enumeration entries = Zip.getentries (); entries.hasmoreelements ();) {
ZipEntry entry = (zipentry) entries.nextelement ();
String zipentryname = Entry.getname ();
InputStream in = Zip.getinputstream ((org.apache.tools.zip.ZipEntry) entry);
String Outpath = (descdir+zipentryname). ReplaceAll ("\\*", "/");;
To determine if a path exists, create a file path if it does not exist
File File = new file (outpath.substring (0, Outpath.lastindexof ('/')));
if (!file.exists ()) {
File.mkdirs ();
}
Determine whether the full path of the file is a folder, if it is uploaded above, do not need to unzip
if (new File (Outpath). Isdirectory ()) {
Continue
}
Output file path information
System.out.println (Outpath);

OutputStream out = new FileOutputStream (Outpath);
byte[] buf1 = new byte[1024];
int Len;
while ((Len=in.read (BUF1)) >0) {
Out.write (Buf1,0,len);
}
In.close ();
Out.close ();
}
System.out.println ("****************** decompression completed ********************");
}

public static void Main (string[] args) throws Exception {

/**
* Extract Files
*/
File ZipFile = new file ("D://zhouzhiwei.zip"); Zip Package Address
String Path = "d://zipfile/"; Be extracted to this path
Unzipfiles (ZipFile, path);
}
}

Java Decompression zip archive package

Related Article

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.