Java Zip Archive Usage Summary

Source: Internet
Author: User

Java Web Development, sometimes encountered to compress multiple files or folders into a. zip file for the front-end download. Java's JDK provides a java.util.zip interface for everyone to use. Such as:

This is the interface provided by the Java JDK, but when you compress a file or folder, how do you use the interface above? Below I give a few related interfaces, these interfaces are compressed files or folders under the process of using.

Java.util.zip.zipentry;java.util.zip.zipoutputstream;

The following compression process is mainly through the two interfaces to compress files or folders;

The following first gives the source code, followed by the interface in detail the method:

Package Ziputil;import Java.io.bufferedinputstream;import Java.io.file;import java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.util.zip.crc32;import Java.util.zip.checkedoutputstream;import Java.util.zip.zipentry;import java.util.zip.zipoutputstream;/** * Learn to use Java.util.zip compressed files or folders * @author LHM * */public class Ziputil {/** * @param args Main method */public static void main (string[ ] args) {//TODO auto-generated method stub//The first parameter is the source path that needs to be compressed, the second parameter is the destination path of the compressed file, this side needs to add the compressed file name to compress ("H:\\zip/scala", "H:\\zip/oo.zip");} /**S * Compressed file * @param srcfilepath Compressed source path * @param destfilepath compression destination path */public static void compress (String srcfilepath, Str        ing Destfilepath) {//File src = new File (Srcfilepath);        if (!src.exists ()) {throw new RuntimeException (Srcfilepath + "does not exist");        } File ZipFile = new file (Destfilepath);            try {fileoutputstream fos = new FileOutputStream (ZipFile); Zipoutputstream Zos = NEW Zipoutputstream (FOS);            String BaseDir = "";            Compressbytype (SRC, Zos, baseDir);        Zos.close ();        } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); }}/** * is compressed according to the type of the original path. The file path compresses the file directly, * @param src * @param zos * @param baseDir */private static void Compressbytype (file src, Zipoutputstream z        Os,string BaseDir) {if (!src.exists ()) return;        SYSTEM.OUT.PRINTLN ("Compressed path" + BaseDir + src.getname ()); Determine if the file is a file, if it is a file call Compressfile method, if it is a path, then call the Compressdir method; if (Src.isfile ()) {//src is a file, call this method comp                    Ressfile (SRC, Zos, baseDir);        The Else if (Src.isdirectory ()) {//src is a folder that calls this method Compressdir (SRC, Zos, baseDir);        }}/** * zip file */private static void Compressfile (file file, Zipoutputstream zos,string baseDir) {        if (!file.exists ()) return; try {bufferedinputstReam bis = new Bufferedinputstream (new FileInputStream (file));            ZipEntry entry = new ZipEntry (BaseDir + file.getname ());            Zos.putnextentry (entry);            int count;            byte[] buf = new byte[1024];            while ((count = Bis.read (BUF))! =-1) {zos.write (buf, 0, Count);        } bis.close (); } catch (Exception e) {//Todo:handle Exception}}/** * compressed folder */private static V        OID Compressdir (File dir, Zipoutputstream zos,string baseDir) {if (!dir.exists ()) return;        file[] files = dir.listfiles (); if (Files.length = = 0) {try {zos.putnextentry (new ZipEntry (BaseDir + dir.getname () +file.separato            R));            } catch (IOException e) {e.printstacktrace (); }} for (file file:files) {compressbytype (file, Zos, BaseDir + dir.getname () + file.separator)        ; }}}

java.util.zip-Compression Process:

The file is loaded into the file stream FileInputStream first, and then the file stream is entered into the Zipoutputstream;

Source file path: H:\\zip/scala, Destination compressed file zip path: h:\\zip/oo.zip

Java Zip Archive Usage Summary

Related Article

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.