A compressed and decompressed class

zhaozj2021-02-16  57

Import java.util.zip. *; import java.io. *;

Class testzip {public void zip (string zipfilename, string infutfile) throw (zipfilename, new file (inputfile);}

public void zip (String zipFileName, File inputFile) throws Exception {ZipOutputStream out = new ZipOutputStream (new FileOutputStream (zipFileName)); zip (out, inputFile, ""); System.out.println ( "zip done"); out. CLOSE ();

public void unzip (String zipFileName, String outputDirectory) throws Exception {ZipInputStream in = new ZipInputStream (new FileInputStream (zipFileName)); ZipEntry z; while (! (z = in.getNextEntry ()) = null) {System.out.println ("unziping" z.getname ()); if (z.Indirectory ()) {string name = z.getname (); Name = name.substring (0, name.Length () - 1); file f = new File (outputDirectory File.separator name); f.mkdir (); System.out.println ( "mkdir" outputDirectory File.separator name);} else {File f = new File (outputDirectory File. Separator Z.getName ()); f.createNewFile (); fileoutputstream out = new fileoutputstream (f); int b; while ((b = in.read ())! = -1) OUT .write (b); out.close ();}}

In.close ();

Public void zip (zipoutputstream out, file f, string base) throws exception {system.out.println ("zipping" f.getname ()); if (f.Indirectory ()) {file [] fl = f.listfiles (); Out.putnextentry (new zipentry); base = base.Length () == 0? ": Base " / "; for (int i = 0; i

Public static void main (string [] args) {try {testzip t = new testzip (); t.zip ("c: //test.zip", "c: // Test"); T. Enzip ("C : //test.zip "," C: // Test2 ");} catch (exception e) {E.PrintStackTrace (System.out);}}}

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

New Post(0)