Java Extract zip-extract multiple file or folder instances _java

Source: Internet
Author: User

Java extract zip-Multiple files (including folders), as follows:

Compress multiple files and folders to extract complex file directories.

The compression method uses a variable parameter that compresses 1 to many files ... Can write an array of ways or write to the parameter list ...

Zipfiles (Zip, "abc", New File ("D:/english"), New File ("d:/release data. xls"));

Test file directory Structure:

Compressed content of the test: Chinese folder and two Excel files of the same sibling

file[] files = new file[]{new file ("D:/english"), New File ("d:/release data. xls"), New file ("d:/Chinese name. xls")};

Here is the compressed code:

/** * Compressed Files-because out to be outside the recursive call, so encapsulate a method to * Invoke Zipfiles (Zipoutputstream out,string path,file ... srcfiles) * @param zip * @par AM Path * @param srcfiles * @throws ioexception * @author isea533/public static void Zipfiles (File zip,string Path
 , File ... srcfiles) throws ioexception{zipoutputstream out = new Zipoutputstream (new FileOutputStream (Zip));
 Ziptest.zipfiles (Out,path,srcfiles);
 Out.close ();
 SYSTEM.OUT.PRINTLN ("***************** compression complete *******************"); /** * Compressed File-file * @param zipfile zip file * @param srcfiles Compressed source file * @author isea533/public static void Zipfiles (
 Zipoutputstream out,string path,file ... srcfiles) {path = Path.replaceall ("\\*", "/");
 if (!path.endswith ("/")) {path+= "/";
 } byte[] buf = new byte[1024]; 
   try {for (int i=0;i<srcfiles.length;i++) {if (Srcfiles[i].isdirectory ()) {file[] files = srcfiles[i].listfiles ();
   String Srcpath = Srcfiles[i].getname ();
   Srcpath = Srcpath.replaceall ("\\*", "/");
   if (!srcpath.endswith ("/")) {srcpath+= "/";
   } out.putnextentry (New ZipEntry (Path+srcpath));
  Zipfiles (Out,path+srcpath,files);
   } else{FileInputStream in = new FileInputStream (srcfiles[i]);
   System.out.println (path + srcfiles[i].getname ());
   Out.putnextentry (New ZipEntry (path + srcfiles[i].getname ()));
   int Len;
   while ((Len=in.read (BUF)) >0) {out.write (Buf,0,len);
   } out.closeentry ();
  In.close ();
 A catch (Exception e) {e.printstacktrace ());

 }
 }

When compressing, judge the folder and then recursively compress the file.

And then the decompression:

/** * Extract to specified directory * @param zippath * @param descdir * @author isea533/public static void Unzipfiles (String zippath,s
 Tring Descdir) throws ioexception{Unzipfiles (new File (Zippath), descdir); /** * Extract files to the specified directory * @param zipfile * @param descdir * @author isea533/@SuppressWarnings ("rawtypes") public stat
 IC void unzipfiles (file zipfile,string descdir) throws ioexception{file Pathfile = new file (Descdir);
 if (!pathfile.exists ()) {pathfile.mkdirs ();
 } zipfile zip = new ZipFile (ZipFile); For (Enumeration entries = Zip.getentries (); entries.hasmoreelements ();) {ZipEntry entry = (zipentry)
  Entries.nextelement ();
  String zipentryname = Entry.getname ();
  InputStream in = Zip.getinputstream (entry);
  String Outpath = (descdir+zipentryname). ReplaceAll ("\\*", "/");;
  To determine if a path exists and does not exist, create a file path filename = new file (outpath.substring 0, Outpath.lastindexof ('/'));
  if (!file.exists ()) {file.mkdirs ();
//Determine if file full path is a folder, if it is uploaded above, do not need to extract 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 Complete ********************");
 }

When decompression, creates a nonexistent folder for the folder judgment, creates only for the folder, does not unpack. Because decompression is for files, not folders, folders need to be created by themselves.

Test method:

public static void Main (string[] args) throws IOException {
 /**
  * Compressed file */
 file[] files = new File[]{new Fi Le ("D:/english"), New File ("d:/release data. xls"), New file ("d:/Chinese name. xls")};
 File Zip = new file ("d:/compressed. zip");
 Zipfiles (Zip, "abc", files);
 
 /**
  * Extract files
 /File ZipFile = new file ("d:/compressed. zip");
 String Path = "d:/zipfile/";
 Unzipfiles (ZipFile, path);
 

The test method does not do anything with the exception, this is not true, please do not imitate.

Output results:

Abc/english/templete.xls
abc/english/Chinese/csdn/isea/533/abc/templete.xls
abc/english/Chinese/csdn/isea/533/abc/zipfile2/templete.xls
abc/english/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/templete.xls
abc/english/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/templete.xls
abc/english/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/card list. xls
abc/english/Chinese/csdn/isea/533/abc/zipfile2/card list. xls
abc/english/Chinese/csdn/isea/templete.xls
abc/english/Chinese/csdn/isea/card list. xls
abc/english/Chinese/csdn/templete.xls
abc/english/collar card list. xls
abc/release data. xls
abc/Chinese name. xls
Compression Complete *******************
d:/zipfile/abc/Chinese name. xls
d:/zipfile/abc/release data. xls
d:/zipfile/abc/english/collar card list. xls
d:/zipfile/abc/english/Chinese/csdn/templete.xls
d:/zipfile/abc/english/Chinese/csdn/isea/card list. xls
d:/zipfile/abc/english/Chinese/csdn/isea/templete.xls
d:/zipfile/abc/english/Chinese/csdn/isea/533/abc/templete.xls
D:/zipfile/abc/english/templete.xls
d:/zipfile/abc/english/Chinese/csdn/isea/533/abc/zipfile2/templete.xls
d:/zipfile/abc/english/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/templete.xls
d:/zipfile/abc/english/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/templete.xls
d:/zipfile/abc/english/Chinese/csdn/isea/533/abc/zipfile2/zipfile/abc/zipfile2/card list. xls
d:/zipfile/abc/english/Chinese/csdn/isea/533/abc/zipfile2/card list. xls
Decompression Complete ********************

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.