The ant. jar file of apache is required:
Import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. util. enumeration; import org.apache.tools.zip. zipEntry; import org.apache.tools.zip. zipFile; import org.apache.tools.zip. zipOutputStream;/*** ZIP file compression and decompression (use apache ant. jar to handle Chinese garbled characters) * @ author admin_Hzw **/@ SuppressWar Nings ("unchecked ") public class ZipUtil {/*** compress the file into a zip file ** @ param file * the file to be compressed * @ param zipFile * Where the compressed file is stored * @ throws Exception */ public static void zip (File file, file zipFile) throws Exception {ZipOutputStream output = null; try {output = new ZipOutputStream (new FileOutputStream (zipFile); // the top-level directory starts zipFile (output, file, "");} catch (Exception ex) {ex. printStackTrace ();} fin Ally {// close the stream if (output! = Null) {output. flush (); output. close ();}}} /*** the compressed file is in zip format ** @ param output * ZipOutputStream object * @ param file * the file or folder to be compressed * @ param basePath * The entry root directory * @ throws IOException */private static void zipFile (ZipOutputStream output, file file, String basePath) throws IOException {FileInputStream input = null; try {// the file is the directory if (File. isDirectory () {// get the File list in the current directory file list [] = File. listFiles (); BasePath = basePath + (basePath. length () = 0? "": "/") + File. getName (); // recursively compress each File for (File f: list) zipFile (output, f, basePath);} else {// compress the File basePath = (basePath. length () = 0? "": BasePath + "/") + file. getName (); // System. out. println (basePath); output. putNextEntry (new ZipEntry (basePath); input = new FileInputStream (file); int readLen = 0; byte [] buffer = new byte [1024*8]; while (readLen = input. read (buffer, 0, 1024*8 ))! =-1) output. write (buffer, 0, readLen) ;}} catch (Exception ex) {ex. printStackTrace () ;}finally {// close the stream if (input! = Null) input. close ();}} /*** decompress the zip file ** @ param zipFilePath * absolute zip file path * @ param unzipDirectory * decompressed directory * @ throws Exception */public static void unzip (String zipFilePath, string unzipDirectory) throws Exception {// define the input/output stream object InputStream input = null; OutputStream output = null; try {// create a File object file = new File (zipFilePath ); // create a zip file object ZipFile zipFile = new ZipFile (File); file unzipFile = New File (unzipDirectory); if (unzipFile. exists () unzipFile. delete (); unzipFile. createNewFile (); // obtain the Enumeration zipEnum = zipFile. getEntries (); // defines the object ZipEntry entry = null; String entryName = null, path = null; String names [] = null; int length; // read the entry while (zipEnum. hasMoreElements () {// obtain the current entry = (ZipEntry) zipEnum. nextElement (); entryName = new String (entry. getName (); // Use/to separate the entry names = entryName. split ("\/"); length = names. length; path = unzipFile. getAbsolutePath (); for (int v = 0; v <length; v ++) {if (v <length-1) // FileUtil directory before the last directory. createDirectory (path + = "/" + names [v] + "/"); else {// The last if (entryName. endsWith ("/") // is the directory, the FileUtil folder is created. createDirectory (unzipFile. getAbsolutePath () + "/" + entryName); else {// indicates the file, which is output to the file input = zipFile. GetInputStream (entry); output = new FileOutputStream (new File (unzipFile. getAbsolutePath () + "/" + entryName); byte [] buffer = new byte [1024*8]; int readLen = 0; while (readLen = input. read (buffer, 0, 1024*8 ))! =-1) output. write (buffer, 0, readLen) ;}}} catch (Exception ex) {ex. printStackTrace ();} finally {// close the stream if (input! = Null) input. close (); if (output! = Null) {output. flush (); output. close () ;}} public static void main (String [] args) throws Exception {String path = "D:/temp/ipconfig "; // source File path zip (new File (path + ". txt "), new File (path + ". zip "); // compressed file unzip (path + ". zip "," C: // temp "); // decompress }}
File tool class:
Import java. io. file;/*** File tool class * @ author admin_Hzw **/public class FileUtil {public static void createDirectory (String path) throws Exception {File f = new File (path ); if (! F. exists () {f. createNewFile ();}}}