com.crabshue.commons.compress.ZipUtils 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.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
/**
* Utility class for zip files.
*
* @deprecated use {@link Zipper} and {@link Unzipper} instead.
*/
@Deprecated
public class ZipUtils {
private static Logger logger = LoggerFactory.getLogger(ZipUtils.class);
/**
* Unzip a zip file to 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
* @deprecated use {@link Unzipper#unzipFileToFolder(File, File)} instead
*/
@SuppressWarnings("DuplicatedCode")
public static Collection unzipFileToFolder(final File zipFile, final File outputFolder) {
Validate.notNull(zipFile);
Validate.notNull(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, ret);
return ret;
}
/**
* Zip a collection of {@link File} to a zip file.
*
* @param outputFile the zip file.
* @param filesToZip the files to zip.
* @return the zip file.
* @deprecated use {@link Zipper#zipFiles(File, Collection)} instead.
*/
public static File zipFiles(final File outputFile, final Collection filesToZip) {
Validate.notNull(outputFile);
Validate.notNull(filesToZip);
FileSystemUtils.provideFolder(outputFile.getParentFile());
try {
ZipFile zip = new ZipFile(outputFile);
zip.addFiles(new ArrayList(filesToZip), new ZipParameters());
} catch (ZipException e) {
throw new SystemException(CompressErrorType.CANNOT_CREATE_ZIP, e)
.addContextValue(CompressErrorContext.ZIP_FILE, outputFile);
}
logger.info("Zipped files [{}] to zip file [{}]", filesToZip, outputFile);
return outputFile;
}
/**
* Zip a directory to a zip file.
*
* @param zipFile the zip file.
* @param folder the folder to zip.
* @deprecated use {@link Zipper#zipDirectory(File, File)} instead.
*/
public static void zipDirectory(File zipFile, File folder) {
Validate.notNull(zipFile);
Validate.notNull(folder);
logger.info("Zip folder [{}] in file [{}]", folder, zipFile);
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFile));
addToZipDirectory(folder, out, folder.isAbsolute() ? null : folder.getAbsolutePath());
} catch (Exception e) {
logger.error("Error in zipping file [" + zipFile + "] in folder [" + folder + "]", e);
} finally {
IOUtils.closeQuietly(out);
}
}
private static void addToZipDirectory(File dirObj, ZipOutputStream zipOutputStream, String basePath) {
Validate.notNull(dirObj);
Validate.notNull(zipOutputStream);
File[] files = dirObj.listFiles();
Validate.notNull(files);
byte[] tmpBuf = new byte[1024];
if (Objects.nonNull(files)) {
for (File file : files) {
if (file.isDirectory()) {
addToZipDirectory(file, zipOutputStream, basePath);
continue;
}
FileInputStream in = null;
try {
in = new FileInputStream(file.getAbsolutePath());
logger.info(" Adding file: " + file.getAbsolutePath());
if (StringUtils.isNotEmpty(basePath)) {
if (file.getAbsolutePath().startsWith(basePath) && file.getAbsolutePath().length() > basePath.length()) {
zipOutputStream.putNextEntry(new ZipEntry(file.getAbsolutePath().substring(basePath.length() + 1)));
} else {
zipOutputStream.putNextEntry(new ZipEntry(file.getAbsolutePath()));
}
} else {
zipOutputStream.putNextEntry(new ZipEntry(file.getAbsolutePath()));
}
int len;
while ((len = in.read(tmpBuf)) > 0) {
zipOutputStream.write(tmpBuf, 0, len);
}
zipOutputStream.closeEntry();
} catch (Exception e) {
logger.error("Error including file [{}] in zip stream [{}] with: [{}]", file, zipOutputStream, e);
throw new SystemException(CompressErrorType.CANNOT_CREATE_ZIP, "Error including file in zip stream", e);
} finally {
IOUtils.closeQuietly(in);
}
}
}
}
}