com.crabshue.commons.compress.Unzipper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-compress Show documentation
Show all versions of commons-compress Show documentation
Library for handling compressed archive files.
The newest version!
package com.crabshue.commons.compress;
import java.io.File;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import com.crabshue.commons.compress.exceptions.CompressErrorContext;
import com.crabshue.commons.compress.exceptions.CompressErrorType;
import com.crabshue.commons.exceptions.ApplicationException;
import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.file.FileSystemUtils;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
/**
* Unzip operations.
*/
@Slf4j
public class Unzipper {
/**
* Unzip a zip file into a given folder and return the collection of files (not folders) extracted
*
* @param zipFile the zip file
* @param outputFolder the output folder
* @return the collection of files extracted
*/
public static Collection unzipFileToFolder(@NonNull final File zipFile,
@NonNull final File outputFolder) {
if (!zipFile.exists()) {
throw new ApplicationException(CompressErrorType.ZIP_FILE_DOES_NOT_EXIST)
.addContextValue(CompressErrorContext.ZIP_FILE, zipFile);
}
FileSystemUtils.provideFolder(outputFolder);
try {
ZipFile zip = new ZipFile(zipFile);
zip.extractAll(outputFolder.getAbsolutePath());
} catch (ZipException e) {
throw new SystemException(CompressErrorType.CANNOT_READ_ZIP, e)
.addContextValue(CompressErrorContext.ZIP_FILE, zipFile);
}
Collection ret = FileUtils.listFiles(outputFolder, null, true);
logger.info("Extracted zip file [{}] to output folder [{}] ", zipFile, outputFolder);
logger.debug("Content: [{}] ", ret);
return ret;
}
}