Manipulating Zip files in Java, compressing/decompressing
Last Update:2017-02-28
Source: Internet
Author: User
Compression can be reproduced at will, but please specify the source and author
Sonymusic
2003.05.28
==========================================================================
operation of Zip files in Java, compression/decompression
package test.nothing;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import com.beaconsystem.util.*;
import junit.framework.TestCase;
/**
* @author Sonymusic
*
* is used to test Java.util.zip package compression and decompression file Zip file example.
* is written based on JUnit, includes two test methods, and three helper methods.
* Note that the operation during the use of all the flow, so not only can read and write files. This is just a simple example.
*/
public class Testzipop extends TestCase {
/**
* constructor for Testzipop.
* @param arg0
*/
public Testzipop (String arg0) {
super (ARG0);
}
public static void Main (string[] args) {
Junit.textui.TestRunner.run (Testzipop.class);
}
/**
* Zip compression function test.
* Compresses all files in the D:\\temp\\zipout directory along with subdirectories to D:\\temp\\out.zip.
* @throws Exception
*/
public void Testcreatezip () throws exception{
//Compress all files under Basedir, including subdirectories
String basedir= "D:\\temp\\zipout";
List filelist=getsubfiles (New File (BaseDir));
//Compressed filename
zipoutputstream zos=new Zipoutputstream (New FileOutputStream ("D:\\temp\\out.zip"));
ZipEntry Ze=null;
byte[] Buf=new byte[1024];
int readlen=0;
for (int i = 0; I <filelist.size (); i++) {
file f= (file) filelist.get (i);
System.out.print ("adding:" +f.getpath () +f.getname ());
Create a zipentry and set the name and some other properties
ze=new ZipEntry (Getabsfilename (BaseDir, f));
ze.setsize (F.length ());
Ze.settime (F.lastmodified ());
//Add ZipEntry to Zos and write the actual file contents
zos.putnextentry (ze);
InputStream is=new Bufferedinputstream (new FileInputStream (f));
while ((Readlen=is.read (buf, 0, 1024))!=-1) {
zos.write (buf, 0, Readlen);
}
Is.close ();
System.out.println ("Done ...");
}
Zos.close ();
}
/**
* Test the decompression function.
* Extract the D:\\download\\test.zip file into the D:\\temp\\zipout directory.
* @throws Exception
*/
public void Testreadzip () throws exception{
//inputstream is=new Bufferedinputstream (New FileInputStream ());
String basedir= "D:\\temp\\zipout";
ZipFile zfile=new zipfile ("D:\\download\\test.zip");
System.out.println (Zfile.getname ());
enumeration zlist=zfile.entries ();
ZipEntry Ze=null;
byte[] Buf=new byte[1024];
while (zlist.hasmoreelements ()) {
//From ZipFile to get a zipentry
ze= (ZipEntry) zlist.nextelement ();
if (ze.isdirectory ()) {
System.out.println ("Dir:" +ze.getname () + "skipped ...");
continue;
}
System.out.println ("Extracting:" +ze.getname () + "T" +ze.getsize () + "T" +ze.getcompressedsize ());
//To ZipEntry as a parameter to get a inputstream, and write to OutputStream
outputstream os=new Bufferedoutputstream (New FileOutputStream (Getrealfilename, BaseDir ()));
inputstream is=new bufferedinputstream (zfile.getinputstream (Ze));
int readlen=0;
while ((Readlen=is.read (buf, 0, 1024))!=-1) {
Os.write (buf, 0, Readlen);
}
Is.close ();
Os.close ();
System.out.println ("Extracted:" +ze.getname ());
}
Zfile.close ();
}
/**
* Given the root directory, returns the actual file name corresponding to a relative path.
* @param baseDir Specifies the root directory
* @param absfilename relative pathname from the name
in ZipEntry
* @return Java.io.File the actual file
*/
Private File Getrealfilename (string baseDir, String absfilename) {
string[] Dirs=regex.split ("/", absfilename);
//system.out.println (dirs.length);
file Ret=new file (BaseDir);
if (dirs.length>1) {
for (int i = 0; I <dirs.length-1; i++) {
ret=new File (ret, dirs[i]);
}
}
if (!ret.exists ()) {
Ret.mkdirs ();
}
ret=new File (ret, dirs[dirs.length-1]);
return ret;
}
/**
* Given the root directory, returns the relative path of another file name for the path in the zip file.
* @param baseDir java.lang.String root directory
* @param realfilename java.io.File the actual filename
* @return Relative filename
*/
private String Getabsfilename (String baseDir, File realfilename) {
File Real=realfilename;
file Base=new file (BaseDir);
String ret=real.getname ();
while (true) {
Real=real.getparentfile ();
if (real==null) break;
if (real.equals (base)) break;
else{
ret=real.getname () + "/" +ret;
}
}
return ret;
}
/**
* Gets a list of all the files in the specified directory, including subdirectories.
* @param the directory specified by BaseDir File
* @return contains the Java.io.File list
*/
Private List Getsubfiles (File baseDir) {
List ret=new ArrayList ();
//file base=new File (BaseDir);
file[] Tmp=basedir.listfiles ();
for (int i = 0; I <tmp.length; i++) {
if (Tmp[i].isfile ()) {
Ret.add (Tmp[i]);
}
if (tmp[i].isdirectory ()) {
Ret.addall (Getsubfiles (tmp[i));
}
}
return ret;
}
}