Package com.amalgamasimulation.zipper
Class Zipper
java.lang.Object
com.amalgamasimulation.zipper.Zipper
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanExtracts the contents of the specified ZIP archive into the given output folder.static booleanCompresses the given list of files and folders into a single ZIP file.
-
Constructor Details
-
Zipper
public Zipper()
-
-
Method Details
-
zip
Compresses the given list of files and folders into a single ZIP file.This method takes a list of
Fileobjects, which can represent files or directories, and archives them into a zip file specified byzipFilePath. If a directory is provided, its contents are recursively added, preserving the folder structure inside the zip archive.- Parameters:
filesAndFolders- aListofFileobjects representing files and folders to zip. Files or folders that do not exist are skipped.zipFilePath- the destinationFilerepresenting the zip archive to be created.- Returns:
trueif the compression completes successfully;falseotherwise.- Throws:
IllegalArgumentException- iffilesAndFoldersorzipFilePathis 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
Extracts the contents of the specified ZIP archive into the given output folder.This method will unzip the file pointed to by
zipFilePathand extract all entries into the directory specified byoutFolderPath. 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- theFileobject representing the ZIP archive to extract. Must exist and be a readable ZIP file.outFolderPath- theFileobject representing the directory where extracted files and folders will be placed. Created if it does not exist.- Returns:
trueif the extraction completes successfully without critical errors;falseotherwise.- Throws:
IllegalArgumentException- if eitherzipFilePathoroutFolderPathisnull.Example usage:
File zipFile = new File("path/to/archive.zip"); File outputDir = new File("path/to/destination/folder"); boolean success = ZipUtil.unzip(zipFile, outputDir);
-