Java.util.zip package for getting started with extracting files

Source: Internet
Author: User

Java.util.zip package for getting started with extracting files
work idle, inadvertently point a bit of java.util.zip package below some source read a look. Know the scope of its use is for our daily
file compression and decompression operations. Wrote a number of commonly used compressed files (compression of individual files and multiple files), unzip files (unzip individual files and folders)
Little Demo.

Package com.clark.zip;


Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.util.zip.ZipEntry;
Import java.util.zip.ZipException;
Import Java.util.zip.ZipFile;
Import Java.util.zip.ZipInputStream;
Import Java.util.zip.ZipOutputStream;


/**
* Single File compression
* @author Caolipeng
* @date December 22, 2014 2:48:15
* @SingleFileZip. java
* @version 1.0
*/
public class Filezip {

public static void Main (string[] args) {
/*getzipfilename (); */
/** compressing a single file * /
/*string filepath = "F:/iboxcleardb_orcl.sql";
String Zippath = "F:/test/db.zip";
ZipFile (filepath, zippath); * *
/** compress multiple files at once, file in one folder * /
/*string filepath = "e:/work weekly/";
String Zippath = "F:/test/week-job.zip";
Zipmultifile (filepath, zippath); * *
/** Unzip (unzip a single file) */
/*string Zippath = "F:/test/db.zip";
String Outfilepath = "F:/test.sql";
String filename = "Iboxcleardb_orcl.sql";
Zipcontrafile (Zippath, outfilepath, filename); * *
/** Decompression (contains multiple files in a compressed file) */
String Zippath = "F:/test/week-job.zip";
String Outfilepath = "f:/";
Zipcontramultifile (Zippath, Outfilepath);
}

/** Get the file name of the zip file * /
private static void Getzipfilename () {
File File = new file ("F:/test/temp.zip");
ZipFile zipfile = null;
try {
ZipFile = new ZipFile (file);
System.out.println ("Zip file name is:" +zipfile.getname ());
} catch (Zipexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}finally{
if (zipfile!=null) {
try {
Zipfile.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

/* * Compress individual files * /
public static void ZipFile (string filepath, string zippath) {
InputStream input = null;
Zipoutputstream zipout = null;
try {
File File = new file (filepath);
File ZipFile = new file (Zippath);
input = new FileInputStream (file);
ZipOut = new Zipoutputstream (new FileOutputStream (ZipFile));
Zipout.putnextentry (New ZipEntry (File.getname ()));
int temp = 0;
while (temp = Input.read ())! =-1) {
Zipout.write (temp);
}
System.out.println ("Zip" +filepath+ "to" +zippath);
} catch (Exception e) {
E.printstacktrace ();
}finally{
try {
Input.close ();
Zipout.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}

/* * Compress multiple files at once, file in one folder * /
public static void Zipmultifile (string filepath, string zippath) {
try {
File File = new file (filepath);//folder to be compressed
File ZipFile = new file (Zippath);
InputStream input = null;
Zipoutputstream zipout = new Zipoutputstream (new FileOutputStream (ZipFile));
if (File.isdirectory ()) {
file[] files = file.listfiles ();
for (int i = 0; i < files.length; ++i) {
input = new FileInputStream (files[i]);
Zipout.putnextentry (New ZipEntry (File.getname () + File.separator + files[i].getname ()));
int temp = 0;
while (temp = Input.read ())! =-1) {
Zipout.write (temp);
}
Input.close ();
}
}else{//Otherwise, the method of compressing a single file is called
ZipFile (filepath, zippath);
}
Zipout.close ();
System.out.println ("Zip directory is success");
} catch (Exception e) {
E.printstacktrace ();
}
}

/** Unzip (unzip a single file) */
public static void Zipcontrafile (String zippath, String outfilepath, string filename) {
try {
File File = new file (zippath);//Zip files path and file name
File OutFile = new file (outfilepath);//Unzip the path and file name
ZipFile zipfile = new ZipFile (file);
ZipEntry entry = zipfile.getentry (filename);//The extracted file name
InputStream input = Zipfile.getinputstream (entry);
OutputStream output = new FileOutputStream (outFile);
int temp = 0;
while (temp = Input.read ())! =-1) {
Output.write (temp);
}
Input.close ();
Output.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}

/** decompression (contains multiple files in a compressed file) can be used instead of the method above.
* Zipinputstream Class
* When we need to extract multiple files, ZipEntry is not available,
* If you want to manipulate more complex compressed files, we must use the Zipinputstream class
* */
public static void Zipcontramultifile (String zippath, String outzippath) {
Zipinputstream zipinput = null;
ZipFile zipfile = null;
InputStream input = null;
OutputStream output = null;
try {
File File = new file (Zippath);
File outFile = null;
ZipFile = new ZipFile (file);
Zipinput = new Zipinputstream (new FileInputStream (file));
ZipEntry entry = null;
while ((entry = Zipinput.getnextentry ()) = null) {
System.out.println ("Unzip" + entry.getname () + "file");
OutFile = new File (Outzippath + file.separator + entry.getname ());
if (!outfile.getparentfile (). exists ()) {
Outfile.getparentfile (). mkdir ();
}
if (!outfile.exists ()) {
Outfile.createnewfile ();
}
Input = Zipfile.getinputstream (entry);
Output = new FileOutputStream (outFile);
int temp = 0;
while (temp = Input.read ())! =-1) {
Output.write (temp);
}
}
} catch (Exception e) {
E.printstacktrace ();
}finally{
try {
Input.close ();
Output.close ();
Zipinput.close ();
Zipfile.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

Java.util.zip package for getting started with extracting files

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.