org.conqat.lib.commons.filesystem.ZipFileUtils Maven / Gradle / Ivy
Show all versions of teamscale-lib-commons Show documentation
/*
* Copyright (c) CQSE GmbH
*
* 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 org.conqat.lib.commons.filesystem;
import static java.lang.Math.min;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.conqat.lib.commons.string.StringUtils;
/**
* Utility methods which deal with the content of Zip files. (Does not support caching.)
*/
public abstract class ZipFileUtils {
/**
* Write entries in chunks of 1 Mchars.
*
* Due to UTF-8 encoding, one char may be encoded as up to four byte, so in the worst case, thus
* chunk size corresponds to 4 MB.
*/
public static final int CHAR_CHUNK_SIZE = (int) ByteUnit.MEBIBYTES.toBytes(1);
/**
* Reads the content of an entry in a ZipFile.
*
* @throws IOException
* if no entry with the given name exists or no size information is stored for the
* entry.
*/
public static byte[] readZipEntryContent(ZipFile zipFile, String entryName) throws IOException {
ZipArchiveEntry entry = zipFile.getEntry(entryName);
if (entry == null) {
throw new IOException("Entry " + entryName + " does not exist in " + zipFile.getName());
}
int size = (int) entry.getSize();
if (size < 0) {
throw new IOException("Size for entry " + entryName + " not stored in ZIP file " + zipFile.getName());
}
byte[] content = new byte[size];
try (InputStream in = zipFile.getInputStream(entry)) {
FileSystemUtils.safeRead(in, content);
return content;
}
}
/** @return The entry for the given entry name as a {@link String} or null. */
public static String extractEntryAsString(ZipArchiveInputStream zis, String entryName) throws IOException {
ArchiveEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (entry.getName().equals(entryName)) {
byte[] content = FileSystemUtils.readStreamBinary(zis);
return StringUtils.bytesToString(content);
}
}
return null;
}
/** Writes a ZIP entry with the given name and content. */
public static void writeZipEntry(ZipArchiveOutputStream zos, String entryName, CharSequence content)
throws IOException {
zos.putArchiveEntry(new ZipArchiveEntry(entryName));
int beginningOfChunk = 0;
int endOfContent = content.length();
while (beginningOfChunk < endOfContent) {
int endOfChunk = min(beginningOfChunk + CHAR_CHUNK_SIZE, endOfContent);
CharBuffer contentChunk = CharBuffer.wrap(content, beginningOfChunk, endOfChunk);
ByteBuffer encodedChunk = StandardCharsets.UTF_8.encode(contentChunk);
zos.write(encodedChunk.array(), encodedChunk.position(), encodedChunk.limit());
beginningOfChunk = endOfChunk;
}
zos.closeArchiveEntry();
}
}