use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project atlas by alibaba.
the class ZipUtils method unzip.
/**
* <p>
* unzip.
* </p>
*
* @param zipFile a {@link java.io.File} object.
* @param destination a {@link String} object.
* @param encoding a {@link String} object.
* @return a {@link java.util.List} object.
*/
public static List<String> unzip(final File zipFile, final String destination, String encoding) {
List<String> fileNames = new ArrayList<String>();
String dest = destination;
if (!destination.endsWith(File.separator)) {
dest = destination + File.separator;
}
ZipFile file;
try {
file = null;
if (null == encoding)
file = new ZipFile(zipFile);
else
file = new ZipFile(zipFile, encoding);
Enumeration<ZipArchiveEntry> en = file.getEntries();
ZipArchiveEntry ze = null;
while (en.hasMoreElements()) {
ze = en.nextElement();
File f = new File(dest, ze.getName());
if (ze.isDirectory()) {
f.mkdirs();
continue;
} else {
f.getParentFile().mkdirs();
InputStream is = file.getInputStream(ze);
OutputStream os = new FileOutputStream(f);
IOUtils.copy(is, os);
is.close();
os.close();
fileNames.add(f.getAbsolutePath());
}
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
return fileNames;
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project POL-POM-5 by PlayOnLinux.
the class Zip method uncompress.
/**
* Uncompress a tar
*
* @param countingInputStream to count the number of byte extracted
* @param outputDir The directory where files should be extracted
* @return A list of extracted files
* @throws ArchiveException if the process fails
*/
private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream, final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) {
final List<File> uncompressedFiles = new LinkedList<>();
try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("zip", inputStream)) {
ZipArchiveEntry entry;
while ((entry = (ZipArchiveEntry) debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
LOGGER.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
LOGGER.info(String.format("Attempting to createPrefix output directory %s.", outputFile.getAbsolutePath()));
Files.createDirectories(outputFile.toPath());
}
} else {
LOGGER.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
outputFile.getParentFile().mkdirs();
try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) {
IOUtils.copy(debInputStream, outputFileStream);
}
}
uncompressedFiles.add(outputFile);
stateCallback.accept(new ProgressEntity.Builder().withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100).withProgressText("Extracting " + outputFile.getName()).build());
}
return uncompressedFiles;
} catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) {
throw new ArchiveException("Unable to extract the file", e);
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project zm-mailbox by Zimbra.
the class ZipUtil method getZipEntryNameAndSize.
/**
*
* @param inputStream archive input stream
* @param locale - best guess as to locale for the filenames in the archive
* @param seqNo - the order of the item to return (excluding directory entries)
* @return
* @throws IOException
*/
public static ZipNameAndSize getZipEntryNameAndSize(InputStream inputStream, Locale locale, int seqNo) throws IOException {
ZipArchiveInputStream zis = new ZipArchiveInputStream(inputStream, cp437charset.name(), false);
ZipArchiveEntry ze;
int idx = 0;
while ((ze = zis.getNextZipEntry()) != null) {
if (ze.isDirectory()) {
continue;
}
if (idx++ == seqNo) {
String entryName = bestGuessAtEntryName(ze, locale);
return new ZipNameAndSize(entryName, ze.getSize(), zis);
}
}
zis.close();
throw new IOException("file " + seqNo + " not in archive");
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project karaf by apache.
the class ArchiveMojo method addFileToZip.
private void addFileToZip(ZipArchiveOutputStream tOut, Path f, String base) throws IOException {
if (Files.isDirectory(f)) {
String entryName = base + f.getFileName().toString() + "/";
ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
tOut.putArchiveEntry(zipEntry);
tOut.closeArchiveEntry();
try (DirectoryStream<Path> children = Files.newDirectoryStream(f)) {
for (Path child : children) {
addFileToZip(tOut, child, entryName);
}
}
} else if (useSymLinks && Files.isSymbolicLink(f)) {
String entryName = base + f.getFileName().toString();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
zipEntry.setUnixMode(UnixStat.LINK_FLAG | UnixStat.DEFAULT_FILE_PERM);
tOut.putArchiveEntry(zipEntry);
tOut.write(Files.readSymbolicLink(f).toString().getBytes());
tOut.closeArchiveEntry();
} else {
String entryName = base + f.getFileName().toString();
ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
zipEntry.setSize(Files.size(f));
if (entryName.contains("/bin/") || (!usePathPrefix && entryName.startsWith("bin"))) {
if (!entryName.endsWith(".bat")) {
zipEntry.setUnixMode(0755);
} else {
zipEntry.setUnixMode(0644);
}
}
tOut.putArchiveEntry(zipEntry);
Files.copy(f, tOut);
tOut.closeArchiveEntry();
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tika by apache.
the class CXFTestBase method readZipArchive.
protected Map<String, String> readZipArchive(InputStream inputStream) throws IOException {
Map<String, String> data = new HashMap<String, String>();
Path tempFile = writeTemporaryArchiveFile(inputStream, "zip");
ZipFile zip = new ZipFile(tempFile.toFile());
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOUtils.copy(zip.getInputStream(entry), bos);
data.put(entry.getName(), DigestUtils.md5Hex(bos.toByteArray()));
}
zip.close();
Files.delete(tempFile);
return data;
}
Aggregations