Java-compressing folders into zip files and java folders into zip files

Source: Internet
Author: User

Java-compressing folders into zip files and java folders into zip files

Import java. io. bufferedInputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import org.apache.tools.zip. zipEntry; import org.apache.tools.zip. zipOutputStream; /***** @ author hwt **/public class TestDir {/*** compresses the folder and its files into files * @ param args * @ throws IOException */public static void main (String [] args) throws IOException {
// Source folder File = new file ("D:/ziptest ");
// Target file ZipOutputStream zos = new ZipOutputStream (new FileOutputStream ("D:/test.zip"); if (file. isDirectory () {File [] files = file. listFiles (); for (int I = 0; I <files. length; I ++) {BufferedInputStream bis = new BufferedInputStream (new FileInputStream (files [I]); zos. putNextEntry (new ZipEntry (file. getName () + File. separator + files [I]. getName (); while (true) {byte [] B = new byte [100]; int len = bis. read (B); if (len =-1) break; zos. write (B, 0, len);} bis. close () ;}} zos. close ();}}

 

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.