Package com;
Import Java.io.BufferedInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.util.zip.CRC32;
Import Java.util.zip.CheckedOutputStream;
Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipOutputStream;
public class Zipcompressor {
static final int BUFFER = 8192;
Private File ZipFile;
Public Zipcompressor (String pathName) {
ZipFile = new File (pathName);
}
public void Compress (String srcpathname) {
file file = new file (srcpathname);
if (!file.exists ())
throw new RuntimeException (Srcpathname + "does not exist!) ");
try {
fileoutputstream fileoutputstream = new FileOutputStream (zipFile);
checkedoutputstream cos = new Checkedoutputstream (FileOutputStream, New CRC32 ());
zipoutputstream out = new Zipoutputstream (COS);
string basedir = "";
compress (file, out, basedir);
out.close ();
} catch (Exception e) {
throw new runtimeexception (e);
}
}
private void compress (file file, zipoutputstream out, String basedir) {
/* Determine if it is a directory or a file */
if (File.isdirectory ()) {
System.out.println ("Compression:" + Basedir + file.getname ());
This.compressdirectory (file, out, basedir);
} else {
System.out.println ("Compression:" + Basedir + file.getname ());
This.compressfile (file, out, basedir);
}
}
/** Compress a directory */
private void Compressdirectory (File dir, zipoutputstream out, String basedir) {
if (!dir.exists ())
Return
file[] files = dir.listfiles ();
for (int i = 0; i < files.length; i++) {
/* Recursive */
Compress (Files[i], out, Basedir + dir.getname () + "/");
}
}
/** Compress a file */
private void Compressfile (file file, zipoutputstream out, String basedir) {
if (!file.exists ()) {
Return
}
try {
Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (file));
ZipEntry entry = new ZipEntry (Basedir + file.getname ());
Out.putnextentry (entry);
int count;
byte data[] = new Byte[buffer];
while ((count = bis.read (data, 0, BUFFER))! =-1) {
Out.write (data, 0, count);
}
Bis.close ();
} catch (Exception e) {
throw new RuntimeException (e);
}
}
}
//-------------------------------------------------------------------------------------------------------------
Here is the test
Package com;
Import Java.io.File;
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;
public class Test {
public static void Main (string[] args) {
1-----------------------------------------------------Creating a compressed file
Compress the F:/code directory into a f:/test.zip file
Zipcompressor ZC = new Zipcompressor ("f:/Chinese. zip");
Zc.compress ("F:/code");
2-----------------------------------------------------Unzip the file
FileOutputStream fileout;
File file;
InputStream InputStream;
try {
Files to unzip f:/Chinese. zip
ZipFile zipfile = new ZipFile ("f:/Chinese. zip");
//extracted to the specified directory f:/ Self-establishing the relevant code directory
string Path = "f:/";
for (Enumeration entries = Zipfile.getentries (); entries.hasmoreelements ();) {
zipentry entry = (zipentry) entries.nextelement ();
file = new File (path + entry.getname ());
if (Entry.isdirectory ()) {
file.mkdirs ();
} else {
//created if the directory for the specified file does not exist.
file parent = File.getparentfile ();
if (!parent.exists ()) {
parent.mkdirs () ;
}
InputStream = Zipfile.getinputstream (entry);
int readedbytes = 0;
byte[] buf = new byte[1024];
Fileout = new FileOutputStream (file);
while ((Readedbytes = Inputstream.read (buf)) > 0) {
Fileout.write (buf, 0, readedbytes);
}
Fileout.close ();
Inputstream.close ();
}
}
Zipfile.close ();
} catch (IOException IoE) {
Ioe.printstacktrace ();
}
}
}
Java Compressed Files