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 );}}