/ ** * Compressed and decompressed file * / import java.util. *; Import java.util.zip. *; Import java.io. *;
Public class zip {public zip () {}
/ ** * files in the archive directory * @param zipFileName * @param inputFile directory * @throws java.lang.Exception * / public void zip (String zipFileName, String inputFile) throws Exception {zip (zipFileName, new File (inputFile) }
/ ** * archive * @param zipFileName * @param inputFile file * @throws java.lang.Exception * / public void zip (String zipFileName, File inputFile) throws Exception {ZipOutputStream out = new ZipOutputStream (new FileOutputStream (zipFileName)) ZIP (OUT, INPUTFILE, ""); System.out.Println ("Compressed success!"); Out.close ();}
Public void zip (zipoutputstream out, file f, string base) throws exception {system.out.println ("is compressed" f.getname ()); if (f.IsDirectory ()) {file [] fl = f. Listfiles (); out.putnextentry (new zipentry); base = base.Length () == 0? ": base " / "; for (int i = 0; i } / ** * decompression * @param zipFileName * @param outputDirectory output directory * @throws java.lang.Exception * / 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 ("Unzip" z.getName ()); if (z.IsDirectory ()) {String Name = z.getname (); name = name.substring (0, name.Length () - 1); file f = new file (OutputDirectory file.seParetor Name); f.mkdir (); system.out.println ( 'create the directory " 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 static void main (string [] args) {Try {zip t = new zip (); // t.zip ("D: //ideaprojects.zip", "D: // Ideaprojects"); // t. Unzip ("D: //ddd//API.jar", "D: // DDD //");} catch (exception e) {E.PrintStackTrace (System.out);}}