Java-compressing a file into a zip file and compressing a java file into a zip file

Source: Internet
Author: User

Java-compressing a file into a zip file and compressing a java file into a zip file

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.util.zip. zipEntry; import java.util.zip. zipOutputStream;/*** package the files in the folder * into a zip compressed file ** @ author hwt **/public class FileToZip {private FileToZip () {}/*** will be stored in The source file under the sourceFilePath directory is packaged into a zip file named fileName and stored in the zipFilePath * @ param sourceFilePath: Path of the file to be compressed * @ param zipFilePath: storage path after compression * @ param fileName: name of the compressed file * @ return */public static String fileToZip (String sourceFilePath, String zipFilePath, String fileName) {File sourceFile = new File (sourceFilePath); FileInputStream FCM = null; BufferedInputStream bis = null; FileOutputStream fos = null; ZipOutputSt Ream zos = null; // determines whether the source file exists if (! SourceFile. exists () {System. out. println ("directory of the file to be compressed:" + sourceFilePath + "does not exist. ");} else {try {File zipFile = new File (zipFilePath +"/"+ fileName + ". zip ");
// Determine whether the compressed file will repeat if (zipFile. exists () {System. out. println (zipFilePath + "directory already exists name" + fileName + ". zip "+" file ");} else {
// Obtain all files in the source folder [] sourceFiles = sourceFile. listFiles (); if (null = sourceFiles | sourceFiles. length <1) {System. out. println ("directory of the file to be compressed:" + sourceFilePath + "there is no file in it, no compression is required. ");} else {fos = new FileOutputStream (zipFile); zos = new ZipOutputStream (new BufferedOutputStream (fos); byte [] bufs = new byte [1024*10]; for (int I = 0; I <sourceFiles. length; I ++) {// create a ZIP object and add it to the ZIP package ZipEntry zipEntry = new ZipEnt Ry (sourceFiles [I]. getName (); zos. putNextEntry (zipEntry); // read the file to be compressed and write it into the compressed package. In the package, the file is saved as new FileInputStream (sourceFiles [I]); bis = new BufferedInputStream (FCM, 1024*10 ); int read = 0; while (read = bis. read (bufs, 0, 1024*10 ))! =-1) {zos. write (bufs, 0, read) ;}}} catch (FileNotFoundException e) {e. printStackTrace (); throw new RuntimeException (e);} catch (IOException e) {e. printStackTrace (); throw new RuntimeException (e);} finally {// close the stream try {if (null! = Bis) bis. close (); if (null! = Zos) zos. close ();} catch (IOException e) {e. printStackTrace (); throw new RuntimeException (e) ;}} return fileName + ". zip ";} public static void main (String [] args) {String sourceFilePath =" D: \ source "; String zipFilePath =" D: \ targetzip "; string fileName = "test"; String fn = FileToZip. fileToZip (sourceFilePath, zipFilePath, fileName); System. out. println (fn );}}

 

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.