Java.util.zip compression and decompression tool class, java decompression tool class
Java.util.zip provides classes for reading and writing standard ZIP files and GZIP files.
The DEFLATE compression algorithm (For ZIP and GZIP file formats) is also used to compress and decompress data.
Compile this tool class by using Jdk. It does not rely on any third-party jar and can be used as needed. The implementation functions are as follows:
1. Directory-level Recursive compression and decompression zip;
2. zip compression and decompression of a single file;
Import java. io. *; import java.util.zip. zipEntry; import java.util.zip. zipInputStream; import java.util.zip. zipOutputStream;/*** use the Java Zip input/output stream to compress and decompress the file */public final class ZipUtil {private ZipUtil () {}/*** compressed File ** @ param filePath path of the File to be compressed * @ return compressed File */public static File zip (String filePath) {File target = null; File source = new File (filePath); if (source. exists () {String sourceName = Source. getName (); String zipName = sourceName. contains (".")? SourceName. substring (0, sourceName. indexOf (". "): sourceName; target = new File (source. getParent (), zipName + ". rar "); if (target. exists () {boolean delete = target. delete (); // delete the old compressed package} FileOutputStream fos = null; ZipOutputStream zos = null; try {fos = new FileOutputStream (target ); zos = new ZipOutputStream (new BufferedOutputStream (fos); addEntry (null, source, zos); // Add the corresponding file Entry} catch (IO Exception e) {throw new RuntimeException (e);} finally {IOUtil. closeQuietly (zos, fos) ;}} return target ;} /*** scan to add a file Entry ** @ param base path * @ param source file * @ param zos Zip file output stream * @ throws IOException */private static void addEntry (String base, file source, ZipOutputStream zos) throws IOException {String entry = (base! = Null )? Base + source. getName (): source. getName (); // classification by directory, for example, aaa/bbb.txt if (source. isDirectory () {File [] files = source. listFiles (); if (files! = Null & files. length> 0) {for (File file: files) {addEntry (entry + "/", file, zos); // recursively list all files in the directory, add file Entry }}else {FileInputStream FCM = null; BufferedInputStream bis = null; try {byte [] buffer = new byte [1024*10]; FCM = new FileInputStream (source); bis = new BufferedInputStream (FCM, buffer. length); int read; zos. putNextEntry (new ZipEntry (entry); // if you only want to compress all the files in the folder, you do not need to compress the parent directory. Name length entry. substring (length) while (read = bis. read (buffer, 0, buffer. length ))! =-1) {zos. write (buffer, 0, read);} zos. closeEntry ();} finally {IOUtil. closeQuietly (bis, FS) ;}}/ *** decompress the file ** @ param filePath: compressed file path */public static void unzip (String filePath) {File source = new File (filePath); if (source. exists () {ZipInputStream zis = null; BufferedOutputStream bos = null; try {zis = new ZipInputStream (new FileInputStream (source); ZipEntry entry; while (entry = z) Is. getNextEntry ())! = Null &&! Entry. isDirectory () {File target = new File (source. getParent (), entry. getName (); if (! Target. getParentFile (). exists () {target. getParentFile (). mkdirs () ;}bos = new BufferedOutputStream (new FileOutputStream (target); int read; byte [] buffer = new byte [1024*10]; while (read = zis. read (buffer, 0, buffer. length ))! =-1) {bos. write (buffer, 0, read);} bos. flush ();} zis. closeEntry ();} catch (IOException e) {throw new RuntimeException (e);} finally {IOUtil. closeQuietly (zis, bos );}}}}
IOUtil:
Import java. io. closeable; import java. io. IOException; /*** IO stream tool class */public class IOUtil {/*** close one or more stream objects * @ param closeables closing Stream object list * @ throws IOException */ public static void close (Closeable... closeables) throws IOException {if (closeables! = Null) {for (Closeable closeable: closeables) {if (closeable! = Null) {closeable. close () ;}}}/*** close one or more stream objects */public static void closeQuietly (Closeable... closeables) {try {close (closeables);} catch (IOException e) {// do nothing }}}