Java implements compressing folders into zip packages

package useful;

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;

public class CompressFolder {

     
    public static void main(String[] args) {         // The folder path to be compressed           String folderPath = "D:\\Project\\Program\\IDEAWorkspace\\myauto\\automation-test\\TestReport\\2023- 06-19-15-20-46";          // The compressed file path and file name generated after compression         String zipFilePath = "D:\\result.zip";         try {             zipFolder(folderPath, zipFilePath);             System.out.println ("Zip file created successfully.");         } catch (IOException e) {             System.out.println("Error creating zip file: " + e.getMessage());         }     }     /**      * Package compressed folder      *      * @param folderPath Folder path      * @param zipFilePath Compressed file path


        


        







 





     * @throws IOException IO exception
     */
    public static void zipFolder(String folderPath, String zipFilePath) throws IOException {         FileOutputStream fos = null;         ZipOutputStream zos = null;         try {     fos = new FileOutputStream(zipFilePath);             zos = new ZipOutp utStream(fos);             // Traverse the entire folder recursively and add to the zip             addFolderToZip("", new File(folderPath), zos);         } finally {             if (zos != null) {                 zos.close();             }             if (fos != null ) {                 fos. close();             }         }     }



       

            











 
    /**
     * Recursively add the folder and its files to the compressed stream
     *
     * @param parentPath parent path
     * @param folder folder
     * @param zos Zip output stream
     * @throws FileNotFoundException file not found exception
     * @throws IOException IO exception
     */
    private static void addFolderToZip(String parentPath, File folder, ZipOutputStream zos) throws FileNotFoundException, IOException {         for (File file : folder.listFiles()) {             if (file.isDirectory()) {                 // recursively add subfiles folder                 addFolderToZip(parentPath + folder.getName() + "/", file, zos);             } else {                 FileInputStream fis = null;






                try {                     fis = new FileInputStream(file);                     // Create a new Zip entry and add the input stream to the Zip package                     ZipEntry zipEntry = new ZipEntry(parentPath + folder.getName() + "/" + file.getName());                     zos .putNextEntry(zipEntry);                     byte[] bytes = new byte[1024];                     int length;                     while ((length = fis.read(bytes)) >= 0) {                         zos.write(bytes, 0, length);                     }                 } finally {                     if (fis != null) {                         fis. close();                     }                 }

                    



                    










            }
        }
    }


}
 

Guess you like

Origin blog.csdn.net/qq_30273575/article/details/131852868