1. Decompression
Package zuo.file.zip.unzip;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipFile;
Import Java.util.zip.ZipInputStream;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.commons.lang3.StringUtils;
/**
* @author
*
*/
public class Readzipfile {
public static void Main (string[] args) throws Exception {
try {
Readzipfile ("D:\\ztree.zip");
} catch (Exception e) {
E.printstacktrace ();
}
}
public static void Readzipfile (String file) throws Exception {
ZipFile ZF = new ZipFile (file);
InputStream in = new Bufferedinputstream (new FileInputStream (file));
Zipinputstream Zin = new Zipinputstream (in);
ZipEntry ze;
while ((Ze = zin.getnextentry ()) = null) {
if (Ze.isdirectory ()) {
System.out.println ("Folder path:---" +ze.getname ());
} else {
System.out.println ("File path:---" +ze.getname ());
String fileName = Ze.getname (). substring (Ze.getname (). LastIndexOf ("/") +1);
System.out.println ("File name:---" +filename);
Have data
if (ze.getsize () > 0) {
Now D: Create a new test empty folder under the packing directory and write the zip data in
Fileutils.copyinputstreamtofile (Zf.getinputstream (ze), New File ("d:\\test/" +picname));
}
}
}
Zin.closeentry ();
}
}
2. Compression
Package zuo.file.zip.zipandunzip;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;
Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipFile;
Import Org.apache.tools.zip.ZipOutputStream;
/**
* @source http://my.oschina.net/woqi/blog/11134
* @author
*
*/
public class Ziputil {
/**
* Compression
* @param src
* Source file or directory
* @param dest
* Compressed file path
* @throws IOException
*/
public static void Zip (string src, String dest) throws IOException {
Zipoutputstream out = null;
try {
File OutFile = new file (dest);
out = new Zipoutputstream (outFile);
File Fileordirectory = new file (src);
if (Fileordirectory.isfile ()) {
Zipfileordirectory (out, Fileordirectory, "");
} else {
file[] entries = Fileordirectory.listfiles ();
for (int i = 0; i < entries.length; i++) {
Zipfileordirectory (out, entries[i], "");
}
}
} catch (IOException ex) {
Ex.printstacktrace ();
} finally {
if (out! = null) {
try {
Out.close ();
} catch (IOException ex) {
}
}
}
}
/**
* Recursively compress files or directories
*
* @param out
* Compressed output Stream object
* @param fileordirectory
* File or directory object to compress
* @param Curpath
* The path of the current compressed entry to specify the prefix for the entry name
* @throws IOException
*/
private static void Zipfileordirectory (Zipoutputstream out, File fileordirectory, String curpath) throws IOException {
FileInputStream in = null;
try {
if (!fileordirectory.isdirectory ()) {
Compress files
byte[] buffer = new byte[4096];
int bytes_read;
in = new FileInputStream (fileordirectory);
ZipEntry entry = new ZipEntry (Curpath + fileordirectory.getname ());
Out.putnextentry (entry);
while ((Bytes_read = in.read (buffer))! =-1) {
Out.write (buffer, 0, bytes_read);
}
Out.closeentry ();
} else {
Compressed directory
file[] entries = Fileordirectory.listfiles ();
for (int i = 0; i < entries.length; i++) {
Recursive compression, update curpaths
Zipfileordirectory (out, entries[i], Curpath + fileordirectory.getname () + "/");
}
}
} catch (IOException ex) {
Ex.printstacktrace ();
Throw ex;
} finally {
if (in = null) {
try {
In.close ();
} catch (IOException ex) {
}
}
}
}
public static void Main (string[] args) {
try {
Ziputil.zip ("F:\\temp", "F:\\test.zip");
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Java uncompressed zip compression zip