com.rapiddweller.common.ZipUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rd-lib-common Show documentation
Show all versions of rd-lib-common Show documentation
'rapiddweller Common' is an open source Java library
forked from Databene Commons by Volker Bergmann.
It provides extensions to the Java core library by utility classes, abstract concepts
and concrete implementations.
/*
* Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rapiddweller.common;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* Provides ZIP-related convenience methods.
* Created: 20.10.2011 15:19:07
*
* @author Volker Bergmann
* @since 0.5.10
*/
public class ZipUtil {
private static final int BUFFER_SIZE = 2048;
private static final Logger LOGGER = LogManager.getLogger(ZipUtil.class);
/**
* Compress and delete.
*
* @param source the source
* @param zipFile the zip file
*/
public static void compressAndDelete(File source, File zipFile) {
try {
compress(source, zipFile);
source.delete();
} catch (IOException e) {
throw new RuntimeException("Unexpected error", e);
}
}
/**
* Compress.
*
* @param source the source
* @param zipFile the zip file
* @throws IOException the io exception
*/
public static void compress(File source, File zipFile) throws IOException {
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
out.setMethod(ZipOutputStream.DEFLATED);
try {
addFileOrDirectory(source, source, out);
out.close();
} catch (IOException e) {
throw new RuntimeException("Zipping the report failed");
}
}
/**
* Print content.
*
* @param zipFile the zip file
*/
public static void printContent(File zipFile) {
ZipInputStream in = null;
try {
in = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
ZipEntry entry;
while ((entry = in.getNextEntry()) != null) {
System.out.println(entry.getName());
}
} catch (IOException e) {
LOGGER.error("Error listing archive content of file " + zipFile, e);
} finally {
IOUtil.close(in);
}
}
// private helpers -------------------------------------------------------------------------------------------------
private static void addFileOrDirectory(File source, File root, ZipOutputStream out) throws IOException {
if (source.isFile()) {
addFile(source, root, out);
} else if (source.isDirectory()) {
addDirectory(source, root, out);
}
}
private static void addDirectory(File source, File root, ZipOutputStream out) throws IOException {
for (File file : source.listFiles()) {
addFileOrDirectory(file, root, out);
}
}
private static void addFile(File source, File root, ZipOutputStream out) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
InputStream in = new BufferedInputStream(new FileInputStream(source));
ZipEntry entry = new ZipEntry(FileUtil.relativePath(root, source));
out.putNextEntry(entry);
int count;
while ((count = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
out.write(buffer, 0, count);
}
in.close();
}
}