Java compressed file tool class ZipUtil usage code example, tool class ziputil

Source: Internet
Author: User

Java compressed file tool class ZipUtil usage code example, tool class ziputil

This example uses the Java Zip input/output stream to compress and decompress files. The first part of the code is used to obtain the file path and change the compressed file name. The details are as follows:

Package com.utility.zip; import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java.util.zip. zipEntry; import java.util.zip. zipInputStream; import java.util.zip. zipOutputStream; import com. utility. io. IOUtil;/*** use the Java Zip input/output stream to compress and decompress the file ** @ author liujiduo **/public f Inal class ZipUtil {private ZipUtil () {// empty}/*** 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 () {// compressed file name. source file name. Zip String zipName = source. getName () + ". zip "; target = new File (source. getParent (), zipName); if (target. exists () {target. delete (); // delete the old file} FileOutputStream fos = Null; ZipOutputStream zos = null; try {fos = new FileOutputStream (target); zos = new ZipOutputStream (new BufferedOutputStream (fos )); // Add the corresponding file EntryaddEntry ("/", source, zos);} catch (IOException e) {throw new RuntimeException (e);} finally {IOUtil. closeQuietly (zos, fos) ;}} return target ;} /*** scan to add a file Entry ** @ param base * base path ** @ param source * source File * @ param zos * Zip file output stream * @ throws IOException */priv Ate static void addEntry (String base, File source, ZipOutputStream zos) throws IOException {// classification by directory, for example:/aaa/bbb.txt String entry = base + source. getName (); if (source. isDirectory () {for (File file: source. listFiles () {// recursively list all files in the directory, add the file EntryaddEntry (entry + "/", file, zos) ;}} else {FileInputStream FCM = null; bufferedInputStream bis = null; try {byte [] buffer = new byte [1024*10]; FD = new File InputStream (source); bis = new BufferedInputStream (FCM, buffer. length); int read = 0; zos. putNextEntry (new ZipEntry (entry); while (read = bis. read (buffer, 0, buffer. length ))! =-1) {zos. write (buffer, 0, read);} zos. closeEntry ();} finally {IOUtil. closeQuietly (bis, fiis) ;}}/ *** 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 = null; while (entry = zis. getNext Entry ())! = Null &&! Entry. isDirectory () {File target = new File (source. getParent (), entry. getName (); if (! Target. getParentFile (). exists () {// create the target file parent directory. getParentFile (). mkdirs ();} // Write File bos = new BufferedOutputStream (new FileOutputStream (target); int read = 0; 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) ;}} public static void main (String [] args) {String targetPath = "E :\\ Win7 Wallpaper "; file file = ZipUtil.zip (targetPath); System. out. println (file); ZipUtil. unzip ("F :\\ win7wallpaper .zip ");}}

The following is a Java language description for disabling one or more stream objects through the I/O Stream tool class. It gets a list of closed stream objects, as shown below:

Package com. utility. io; import java. io. closeable; import java. io. IOException; /*** IO stream tool class ** @ author liujiduo **/public class IOUtil {/*** close one or more stream objects ** @ param closeables * 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 ** @ param closeables * List of stream objects that can be closed */public static void closeQuietly (Closeable... closeables) {try {close (closeables);} catch (IOException e) {// do nothing }}}

Summary

The above is all the content of this article on the Java compressed file tool class ZipUtil usage code example, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message.

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.