Java- Operates ZIP files in Java, compresseddecompressed

xiaoxiao2021-03-06  56

Operate ZIP files, compression / decompression in Java

Package test.nothing;

Import java.io. *;

Import java.util. *;

Import java.util.zip. *;

Import com.beaconsystem.util. *;

Import junit.framework.testcase;

/ **

* @Author Sonymusic

*

* Examples used to test the Java.util.zip package compression and decompress file ZIP files.

* Based on JUnit writing, including two Test methods, and three auxiliary methods.

* Notes all the full stream during the use of the use, so it is not only read or written. 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.

* Complicated all the files in the D: // Temp // zipout directory to D: //temp //out.zip.

* @Throws Exception

* /

Public void testcreatezip () throws exception {

// Compress Basedir all files, including subdirectory

String basedir = "d: // Temp // zipout";

List filelist = getSubfiles (New file (basedir));

// Compressed file name

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

File f = (file) filelist.get (i);

System.out.print ("Adding:" f.getPath () f.getname ());

// Create a zipentry and set some properties from Name and 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 content

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.

* Unzip D: //download/test.zip file to 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.hasmorelements ()) {

/ / Get a zipentry from zipfile

z = (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 ());

// Get an InputStream as the parameter as the parameter and write to OutputStream

OutputStream Os = New BufferedoutputStream (New FileoutputStream (getRealFileName (Basedir, Ze.getName ()))))

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 and return a relative path corresponding to the actual file name.

* @Param basedir Specifies the root directory

* @Param AbsFileName relative path name, from Name in Zipentry

* @Return java.io.file 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

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, used in the path in the zip file.

* @Param Basedir java.lang.string root directory

* @Param RealFileName Java.io.file actual file name

* @Return relative file name

* /

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 () "/" reta;

}

}

Return Ret;

}

/ **

* A list of all files in the specified directory, including subdirectories.

* @Param basedir file specified by the directory

* @Return contains List of java.io.file

* /

Private List GetSubfiles (File Basedir) {

List ret = new arraylist ();

// file base = new file (basedir);

File [] TMP = Basedir.ListFiles ();

For (int i = 0; i

IF (TMP [i] .isfile ()) {

Ret.Add (TMP [i]);

}

IF (TMP [i] .Indirectory ()) {

Ret.addall (GetSubfiles);

}

}

Return Ret;

}

}

转载请注明原文地址:https://www.9cbs.com/read-83285.html

New Post(0)