use of org.apache.tools.zip.ZipFile in project gradle by gradle.
the class ZipFileTree method visit.
public void visit(FileVisitor visitor) {
if (!zipFile.exists()) {
throw new InvalidUserDataException(String.format("Cannot expand %s as it does not exist.", getDisplayName()));
}
if (!zipFile.isFile()) {
throw new InvalidUserDataException(String.format("Cannot expand %s as it is not a file.", getDisplayName()));
}
AtomicBoolean stopFlag = new AtomicBoolean();
try {
ZipFile zip = new ZipFile(zipFile);
try {
// The iteration order of zip.getEntries() is based on the hash of the zip entry. This isn't much use
// to us. So, collect the entries in a map and iterate over them in alphabetical order.
Map<String, ZipEntry> entriesByName = new TreeMap<String, ZipEntry>();
Enumeration entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
entriesByName.put(entry.getName(), entry);
}
Iterator<ZipEntry> sortedEntries = entriesByName.values().iterator();
while (!stopFlag.get() && sortedEntries.hasNext()) {
ZipEntry entry = sortedEntries.next();
if (entry.isDirectory()) {
visitor.visitDir(new DetailsImpl(entry, zip, stopFlag, chmod));
} else {
visitor.visitFile(new DetailsImpl(entry, zip, stopFlag, chmod));
}
}
} finally {
zip.close();
}
} catch (Exception e) {
throw new GradleException(String.format("Could not expand %s.", getDisplayName()), e);
}
}
use of org.apache.tools.zip.ZipFile in project libresonic by Libresonic.
the class UploadController method unzip.
private void unzip(File file, List<File> unzippedFiles) throws Exception {
LOG.info("Unzipping " + file);
ZipFile zipFile = new ZipFile(file);
try {
Enumeration<?> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
File entryFile = new File(file.getParentFile(), entry.getName());
if (!entry.isDirectory()) {
if (!securityService.isUploadAllowed(entryFile)) {
throw new Exception("Permission denied: " + StringUtil.toHtml(entryFile.getPath()));
}
entryFile.getParentFile().mkdirs();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = zipFile.getInputStream(entry);
outputStream = new FileOutputStream(entryFile);
byte[] buf = new byte[8192];
while (true) {
int n = inputStream.read(buf);
if (n == -1) {
break;
}
outputStream.write(buf, 0, n);
}
LOG.info("Unzipped " + entryFile);
unzippedFiles.add(entryFile);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
}
}
zipFile.close();
file.delete();
} finally {
zipFile.close();
}
}
use of org.apache.tools.zip.ZipFile in project tdi-studio-se by Talend.
the class ZipFileUtils method unZipFile.
private static void unZipFile(File destFile, ZipFile zipFile, ZipEntry entry) throws IOException {
InputStream inputStream;
FileOutputStream fileOut;
if (entry.isDirectory()) {
destFile.mkdirs();
} else {
File parent = destFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
inputStream = zipFile.getInputStream(entry);
fileOut = new FileOutputStream(destFile);
byte[] buf = new byte[bufSize];
int readedBytes;
while ((readedBytes = inputStream.read(buf)) > 0) {
fileOut.write(buf, 0, readedBytes);
}
fileOut.close();
inputStream.close();
}
}
use of org.apache.tools.zip.ZipFile in project tdi-studio-se by Talend.
the class ZipFileUtils method unZip.
public static String unZip(String unZipFileName, String destFileName) {
File unzipFile = new File(unZipFileName);
if (destFileName == null || destFileName.trim().length() == 0) {
destFileName = unzipFile.getParent();
}
File destFile;
ZipFile zipFile = null;
try {
zipFile = new ZipFile(unzipFile, "GBK");
for (Enumeration entries = zipFile.getEntries(); entries.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) entries.nextElement();
destFile = new File(destFileName, entry.getName());
unZipFile(destFile, zipFile, entry);
}
} catch (Exception e) {
ExceptionHandler.process(e);
return e.getMessage();
} finally {
try {
assert zipFile != null;
zipFile.close();
} catch (Exception e) {
ExceptionHandler.process(e);
}
}
return null;
}
Aggregations