Java read zip file Java Read compressed file extract zip inside the specified format file

Source: Internet
Author: User
On the internet to see a lot of examples, the result is very disappointing, so decided to write an example, to provide you with reference to study; no more nonsense. See examples directly:

Package com.em.test.client;

Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipFile;

/**
* Read the files in the compressed package (. zip)
*
* @author TDW
*
*/
public class Testzip {

public static void Main (String args[]) {
String file = "C://images.zip";
String saverootdirectory= "c://test/";
Zipfileread (file,saverootdirectory);
}

private static void Zipfileread (String file,string saverootdirectory) {
try {
ZipFile zipfile = new ZipFile (file);
@SuppressWarnings ("Unchecked")
Enumeration<zipentry> enu = (enumeration<zipentry>) zipfile.entries ();
while (Enu.hasmoreelements ()) {
ZipEntry zipelement = (zipentry) enu.nextelement ();
InputStream read = Zipfile.getinputstream (zipelement);
String fileName = Zipelement.getname ();
if (filename!= null && filename.indexof (".")!=-1) {//is a file (file with path such as:/images/a.jpg)
Execute (zipelement,read,saverootdirectory);
}
}
catch (Exception e) {
E.printstacktrace ();
}
}

private static void execute (ZipEntry ze, inputstream read,string saverootdirectory)
Throws FileNotFoundException, IOException {
If you only read the picture, make your own decision OK.
String fileName = Ze.getname ();
if (Filename.lastindexof (". jpg")!=-1 | | | filename.lastindexof (". bmp")!=-1
//            || Filename.lastindexof (". jpeg")!=-1) {//Specify the file format to extract (these formats can be extracted in a collection or string array via parameter passing in, method more general)
File File = new file (saverootdirectory + fileName);
if (!file.exists ()) {
File Rootdirectoryfile = new file (File.getparent ());
Create a table of contents
if (!rootdirectoryfile.exists ()) {
Boolean ifsuccess = Rootdirectoryfile.mkdirs ();
if (ifsuccess) {
System.out.println ("Folder creation successful!");
} else {
System.out.println ("File creation failed!");
}
}
Create a file
try {
File.createnewfile ();
catch (IOException e) {
E.printstacktrace ();
}
}

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.