Class Zipper

java.lang.Object
com.amalgamasimulation.zipper.Zipper

public class Zipper extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    unzip(File zipFilePath, File outFolderPath)
    Extracts the contents of the specified ZIP archive into the given output folder.
    static boolean
    zip(List<File> filesAndFolders, File zipFilePath)
    Compresses the given list of files and folders into a single ZIP file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Zipper

      public Zipper()
  • Method Details

    • zip

      public static boolean zip(List<File> filesAndFolders, File zipFilePath)
      Compresses the given list of files and folders into a single ZIP file.

      This method takes a list of File objects, which can represent files or directories, and archives them into a zip file specified by zipFilePath. If a directory is provided, its contents are recursively added, preserving the folder structure inside the zip archive.

      Parameters:
      filesAndFolders - a List of File objects representing files and folders to zip. Files or folders that do not exist are skipped.
      zipFilePath - the destination File representing the zip archive to be created.
      Returns:
      true if the compression completes successfully; false otherwise.
      Throws:
      IllegalArgumentException - if filesAndFolders or zipFilePath is null or empty.

      Example usage:

      
       // Creating File objects from Paths
       Path filePath = Paths.get("path/to/file.txt");
       File file = filePath.toFile();
      
       // For a directory
       Path dirPath = Paths.get("path/to/directory");
       File dir = dirPath.toFile();
      
       // Using the zip method
       List<File> filesToZip = List.of(file, dir);
       File zipOutput = new File("path/to/archive.zip");
       boolean success = ZipUtil.zip(filesToZip, zipOutput);
       
    • unzip

      public static boolean unzip(File zipFilePath, File outFolderPath)
      Extracts the contents of the specified ZIP archive into the given output folder.

      This method will unzip the file pointed to by zipFilePath and extract all entries into the directory specified by outFolderPath. The directory structure contained in the ZIP file is preserved.

      If any file or directory in the archive cannot be extracted, the method prints an error message and continues extracting the other entries.

      Parameters:
      zipFilePath - the File object representing the ZIP archive to extract. Must exist and be a readable ZIP file.
      outFolderPath - the File object representing the directory where extracted files and folders will be placed. Created if it does not exist.
      Returns:
      true if the extraction completes successfully without critical errors; false otherwise.
      Throws:
      IllegalArgumentException - if either zipFilePath or outFolderPath is null.

      Example usage:

      
       File zipFile = new File("path/to/archive.zip");
       File outputDir = new File("path/to/destination/folder");
       boolean success = ZipUtil.unzip(zipFile, outputDir);