Operating ZIP files in Java, compressed / extracting package test.nothing; import java.io. *; Import java.util. *; Import java.util.zip. *; Import com.beaconsystem.util. *; Import junit. Framework.testcase; / *** @Author Sonymusic ** Used to test an example of the Java.util.zip package compression and decompression file ZIP file. * Based on JUnit writing, including two Test methods, and three auxiliary methods. * Note The full flow of operations 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.teestrunner.run (Testzipop.class);} / *** zip compression function test. * Complicate all the files in the D: // Temp // zipout directory Contracts to D: // Temp // out.zip. * @throws exception * / public void testCreatezip () THROWS Exception {// Compressed 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 (New fileoutputstream ("d: //temp//out.zip"); zipentry = null; Byte [] buf = new byte [ 1024]; int = 0; for (int i = 0; I 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 with ZIPENTRY to get an InputStream, 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.Println ("extracted:" ZE. GetName ());} zfile.close ();} / *** Given the root directory, return a relative path corresponding to the actual file name. * @Param basedir Specifies the root directory * @Param AbsFileName relative path name, from Name * @return java.io.file in ZIPENTRY * / 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 Including subdirectory. * @Param basedir file specified directory * @return contains java.io.file List * / private {list ret = new arraylist (); // file base = new file (basedir ); File [] TMP = Basedir.ListFiles (); for (int i = 0; i Posted on December 06, 2004 10:26 PM