use of java.util.zip.ZipEntry in project tomcat by apache.
the class SignCode method getApplicationString.
/**
* Zips the files, base 64 encodes the resulting zip and then returns the
* string. It would be far more efficient to stream this directly to the
* signing server but the files that need to be signed are relatively small
* and this simpler to write.
*
* @param fileNames Modified names of files
* @param files Files to be signed
*/
private static String getApplicationString(List<String> fileNames, List<File> files) throws IOException {
// 16 MB should be more than enough for Tomcat
// TODO: Refactoring this entire class so it uses streaming rather than
// buffering the entire set of files in memory would make it more
// widely useful.
ByteArrayOutputStream baos = new ByteArrayOutputStream(16 * 1024 * 1024);
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
byte[] buf = new byte[32 * 1024];
for (int i = 0; i < files.size(); i++) {
try (FileInputStream fis = new FileInputStream(files.get(i))) {
ZipEntry zipEntry = new ZipEntry(fileNames.get(i));
zos.putNextEntry(zipEntry);
int numRead;
while ((numRead = fis.read(buf)) >= 0) {
zos.write(buf, 0, numRead);
}
}
}
}
return Base64.encodeBase64String(baos.toByteArray());
}
use of java.util.zip.ZipEntry in project cw-omnibus by commonsguy.
the class ZipUtils method unzip.
public static void unzip(File zipFile, File destDir, String subtreeInZip) throws UnzipException, IOException {
if (destDir.exists()) {
deleteContents(destDir);
} else {
destDir.mkdirs();
}
try {
final FileInputStream fis = new FileInputStream(zipFile);
final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
int entries = 0;
long total = 0;
try {
while ((entry = zis.getNextEntry()) != null) {
if (subtreeInZip == null || entry.getName().startsWith(subtreeInZip)) {
int bytesRead;
final byte[] data = new byte[BUFFER_SIZE];
final String zipCanonicalPath = validateZipEntry(entry.getName().substring(subtreeInZip.length()), destDir);
if (entry.isDirectory()) {
new File(zipCanonicalPath).mkdir();
} else {
final FileOutputStream fos = new FileOutputStream(zipCanonicalPath);
final BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);
while (total + BUFFER_SIZE <= DEFAULT_MAX_SIZE && (bytesRead = zis.read(data, 0, BUFFER_SIZE)) != -1) {
dest.write(data, 0, bytesRead);
total += bytesRead;
}
dest.flush();
fos.getFD().sync();
dest.close();
if (total + BUFFER_SIZE > DEFAULT_MAX_SIZE) {
throw new IllegalStateException("Too much output from ZIP");
}
}
zis.closeEntry();
entries++;
if (entries > DEFAULT_MAX_ENTRIES) {
throw new IllegalStateException("Too many entries in ZIP");
}
}
}
} finally {
zis.close();
}
} catch (Throwable t) {
if (destDir.exists()) {
delete(destDir);
}
throw new UnzipException("Problem in unzip operation, rolling back", t);
}
}
use of java.util.zip.ZipEntry in project cglib by cglib.
the class AbstractTransformTask method processJarFile.
protected void processJarFile(File file) throws Exception {
if (verbose) {
log("processing " + file.toURI());
}
File tempFile = File.createTempFile(file.getName(), null, new File(file.getAbsoluteFile().getParent()));
try {
ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
try {
FileOutputStream fout = new FileOutputStream(tempFile);
try {
ZipOutputStream out = new ZipOutputStream(fout);
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
byte[] bytes = getBytes(zip);
if (!entry.isDirectory()) {
DataInputStream din = new DataInputStream(new ByteArrayInputStream(bytes));
if (din.readInt() == CLASS_MAGIC) {
bytes = process(bytes);
} else {
if (verbose) {
log("ignoring " + entry.toString());
}
}
}
ZipEntry outEntry = new ZipEntry(entry.getName());
outEntry.setMethod(entry.getMethod());
outEntry.setComment(entry.getComment());
outEntry.setSize(bytes.length);
if (outEntry.getMethod() == ZipEntry.STORED) {
CRC32 crc = new CRC32();
crc.update(bytes);
outEntry.setCrc(crc.getValue());
outEntry.setCompressedSize(bytes.length);
}
out.putNextEntry(outEntry);
out.write(bytes);
out.closeEntry();
zip.closeEntry();
}
out.close();
} finally {
fout.close();
}
} finally {
zip.close();
}
if (file.delete()) {
File newFile = new File(tempFile.getAbsolutePath());
if (!newFile.renameTo(file)) {
throw new IOException("can not rename " + tempFile + " to " + file);
}
} else {
throw new IOException("can not delete " + file);
}
} finally {
tempFile.delete();
}
}
use of java.util.zip.ZipEntry in project che by eclipse.
the class ZipUtils method unzip.
public static void unzip(InputStream in, File targetDir) throws IOException {
final ZipInputStream zipIn = new ZipInputStream(in);
final byte[] b = new byte[BUF_SIZE];
ZipEntry zipEntry;
while ((zipEntry = zipIn.getNextEntry()) != null) {
final File file = new File(targetDir, zipEntry.getName());
if (!zipEntry.isDirectory()) {
final File parent = file.getParentFile();
if (!parent.exists()) {
if (!parent.mkdirs()) {
throw new IOException("Unable to create parent folder " + parent.getAbsolutePath());
}
}
try (FileOutputStream fos = new FileOutputStream(file)) {
int r;
while ((r = zipIn.read(b)) != -1) {
fos.write(b, 0, r);
}
}
} else {
if (!file.exists()) {
if (!file.mkdirs()) {
throw new IOException("Unable to create folder " + file.getAbsolutePath());
}
}
}
zipIn.closeEntry();
}
}
use of java.util.zip.ZipEntry in project che by eclipse.
the class ZipUtils method addFileEntry.
private static void addFileEntry(ZipOutputStream zipOut, String entryName, File file) throws IOException {
final ZipEntry zipEntryEntry = new ZipEntry(entryName);
zipOut.putNextEntry(zipEntryEntry);
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
final byte[] buf = new byte[BUF_SIZE];
int r;
while ((r = in.read(buf)) != -1) {
zipOut.write(buf, 0, r);
}
}
zipOut.closeEntry();
}
Aggregations