Java-implemented zip compression and decompression tool examples, javazip tool examples

Source: Internet
Author: User

Java-implemented zip compression and decompression tool examples, javazip tool examples

This article describes Java-implemented zip compression and decompression tools. We will share this with you for your reference. The details are as follows:

Import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. util. enumeration; import org.apache.tools.zip. zipEntry; import org.apache.tools.zip. zipFile; import org.apache.tools.zip. zipOutputStream; public CIA Ss ZipUtil {private static final int BUFFEREDSIZE = 1024; /*** compressed file ** @ param zipFileName * path of the compressed package * @ param filePath * path of the folder or file to be compressed * @ param isDelete * whether to delete the source file * @ throws exception */public void zip (String zipFileName, string filePath, boolean isDelete) throws Exception {zip (zipFileName, new File (filePath), isDelete );} /***** compressed file ** @ param zipFileName * path of the compressed package file saved * @ param inputFile * Whether to delete the source File * @ param isDelete * of the folder or File to be compressed * @ throws Exception */public void zip (String zipFileName, File inputFile, boolean isDelete) throws Exception {ZipOutputStream out = new ZipOutputStream (new FileOutputStream (zipFileName); if (! InputFile. exists () {throw new FileNotFoundException ("the file to be compressed is not found in the specified path! ");} Zip (out, inputFile," ", isDelete); out. close ();} /*** recursive compression method ** @ param out * compressed package output stream * @ param f * file to be compressed * @ param base * compressed path * @ param isDelete * whether to delete source File * @ throws Exception */private void zip (ZipOutputStream out, file inputFile, String base, boolean isDelete) throws Exception {if (inputFile. isDirectory () {// If the directory is File [] inputFiles = inputFile. listFiles (); out. putNextEntry (new ZipEnt Ry (base + "/"); base = base. length () = 0? "": Base + "/"; for (int I = 0; I <inputFiles. length; I ++) {zip (out, inputFiles [I], base + inputFiles [I]. getName (), isDelete) ;}} else {// if it is a file if (base. length ()> 0) {out. putNextEntry (new ZipEntry (base);} else {out. putNextEntry (new ZipEntry (inputFile. getName ();} FileInputStream in = new FileInputStream (inputFile); try {int len; byte [] buff = new byte [BUFFEREDSIZE]; while (len = in. Read (buff ))! =-1) {out. write (buff, 0, len) ;}catch (IOException e) {throw e;} finally {in. close () ;}}if (isDelete) {inputFile. delete ();}} /*** decompress ** @ param zipFilePath * compressed package path * @ param fileSavePath * decompressed path * @ param isDelete * whether to delete the source file * @ throws Exception */public void unZip (String zipFilePath, string fileSavePath, boolean isDelete) throws Exception {try {(new File (fileSavePath )). mkdirs (); File F = new File (zipFilePath); if ((! F. exists () & (f. length () <= 0) {throw new Exception ("the file to be decompressed does not exist! ");} ZipFile zipFile = new ZipFile (f); String strPath, gbkPath, strtemp; File tempFile = new File (fileSavePath); // strPath = tempFile from the current directory. getAbsolutePath (); // absolute output location Enumeration <ZipEntry> e = zipFile. getEntries (); while (e. hasMoreElements () {org.apache.tools.zip. zipEntry zipEnt = e. nextElement (); gbkPath = zipEnt. getName (); if (zipEnt. isDirectory () {strtemp = strPath + File. separator + gb KPath; File dir = new File (strtemp); dir. mkdirs (); continue;} else {// read/write file InputStream is = zipFile. getInputStream (zipEnt); BufferedInputStream bis = new BufferedInputStream (is); gbkPath = zipEnt. getName (); strtemp = strPath + File. separator + gbkPath; // create the directory String strsubdir = gbkPath; for (int I = 0; I <strsubdir. length (); I ++) {if (strsubdir. substring (I, I + 1 ). repeated signorecase ("/") {Str Ing temp = strPath + File. separator + strsubdir. substring (0, I); File subdir = new File (temp); if (! Subdir. exists () subdir. mkdir () ;}} FileOutputStream fos = new FileOutputStream (strtemp); BufferedOutputStream bos = new BufferedOutputStream (fos); int len; byte [] buff = new byte [BUFFEREDSIZE]; while (len = bis. read (buff ))! =-1) {bos. write (buff, 0, len);} bos. close (); fos. close () ;}} catch (Exception e) {e. printStackTrace (); throw e;} if (isDelete) {new File (zipFilePath ). delete () ;}// public static void main (String [] args) {// ZipUtil cpr = new ZipUtil (); // try {// cpr.zip ("C: /Users/Lenovo User/Desktop/test .zip "," C:/Users/Lenovo User/Desktop/new folder ", false); // cpr. unZip ("C:/Users/Lenovo User/Desktop/test .zip", "C:/Users/Lenovo User/Desktop/new folder 2", false ); //} catch (Exception e) {// e. printStackTrace ();//}////}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.