use of org.apache.commons.compress.archivers.zip.ZipFile in project buck by facebook.
the class ZipStepTest method minCompressionWritesCorrectZipFile.
/**
* Tests a couple bugs:
* 1) {@link com.facebook.buck.zip.OverwritingZipOutputStream} was writing uncompressed zip
* entries incorrectly.
* 2) {@link ZipStep} wasn't setting the output size when writing uncompressed entries.
*/
@Test
public void minCompressionWritesCorrectZipFile() throws IOException {
Path parent = tmp.newFolder("zipstep");
Path out = parent.resolve("output.zip");
Path toZip = tmp.newFolder("zipdir");
byte[] contents = "hello world".getBytes();
Files.write(toZip.resolve("file1.txt"), contents);
Files.write(toZip.resolve("file2.txt"), contents);
Files.write(toZip.resolve("file3.txt"), contents);
ZipStep step = new ZipStep(filesystem, Paths.get("zipstep/output.zip"), ImmutableSet.of(), false, ZipCompressionLevel.MIN_COMPRESSION_LEVEL, Paths.get("zipdir"));
assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode());
// directory and will verify it's valid.
try (ZipFile zip = new ZipFile(out.toFile())) {
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
ZipArchiveEntry entry1 = entries.nextElement();
assertArrayEquals(contents, ByteStreams.toByteArray(zip.getInputStream(entry1)));
ZipArchiveEntry entry2 = entries.nextElement();
assertArrayEquals(contents, ByteStreams.toByteArray(zip.getInputStream(entry2)));
ZipArchiveEntry entry3 = entries.nextElement();
assertArrayEquals(contents, ByteStreams.toByteArray(zip.getInputStream(entry3)));
}
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project ats-framework by Axway.
the class LocalFileSystemOperations method unzip.
/**
* Unzip file to local or remote machine, if the machine is UNIX-like it will preserve the permissions
*
* @param zipFilePath the zip file path
* @param outputDirPath output directory
* @param isTempDirectory whether the directory is temporary or not. Temporary means that
* it will be automatically deleted only if the virtual machine terminates normally.
* @throws Exception
*/
@Override
public void unzip(String zipFilePath, String outputDirPath) throws FileSystemOperationException {
ZipArchiveEntry zipEntry = null;
File outputDir = new File(outputDirPath);
//check if the dir is created
outputDir.mkdirs();
try (ZipFile zipFile = new ZipFile(zipFilePath)) {
Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries();
int unixPermissions = 0;
while (entries.hasMoreElements()) {
zipEntry = entries.nextElement();
File entryDestination = new File(outputDirPath, zipEntry.getName());
unixPermissions = zipEntry.getUnixMode();
if (zipEntry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
InputStream in = null;
OutputStream out = null;
in = zipFile.getInputStream(zipEntry);
out = new BufferedOutputStream(new FileOutputStream(entryDestination));
IoUtils.copyStream(in, out);
}
if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
//check if the OS is UNIX
// set file/dir permissions, after it is created
Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(octalToDecimal(unixPermissions)));
}
}
} catch (Exception e) {
String errorMsg = "Unable to unzip " + ((zipEntry != null) ? zipEntry.getName() + " from " : "") + zipFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
throw new FileSystemOperationException(errorMsg, e);
}
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project tika by apache.
the class ZipContainerDetector method detectZipFormat.
private static MediaType detectZipFormat(TikaInputStream tis) {
try {
//try opc first because opening a package
//will not necessarily throw an exception for
//truncated files.
MediaType type = detectOPCBased(tis);
if (type != null) {
return type;
}
// TODO: hasFile()?
ZipFile zip = new ZipFile(tis.getFile());
try {
type = detectOpenDocument(zip);
if (type == null) {
type = detectIWork13(zip);
}
if (type == null) {
type = detectIWork(zip);
}
if (type == null) {
type = detectJar(zip);
}
if (type == null) {
type = detectKmz(zip);
}
if (type == null) {
type = detectIpa(zip);
}
if (type != null) {
return type;
}
} finally {
// tis.setOpenContainer(zip);
try {
zip.close();
} catch (IOException e) {
// ignore
}
}
} catch (IOException e) {
// ignore
}
// Fallback: it's still a zip file, we just don't know what kind of one
return MediaType.APPLICATION_ZIP;
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project tika by apache.
the class CXFTestBase method readArchiveText.
protected String readArchiveText(InputStream inputStream) throws IOException {
Path tempFile = writeTemporaryArchiveFile(inputStream, "zip");
ZipFile zip = new ZipFile(tempFile.toFile());
zip.getEntry(UnpackerResource.TEXT_FILENAME);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOUtils.copy(zip.getInputStream(zip.getEntry(UnpackerResource.TEXT_FILENAME)), bos);
zip.close();
Files.delete(tempFile);
return bos.toString(UTF_8.name());
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project stanbol by apache.
the class ConfigUtils method copyCore.
/**
* Copy the configuration of an core.
*
* @param clazzInArchive
* This class is used to identify the archive containing the default configuration. Parsing
* <code>null</code> causes this class to be used and therefore initialises the default core
* configuration contained by the SolrYard bundle.
* @param coreDir
* the target directory for the core
* @param coreName
* the core name or <code>null</code> to directly load the configuration as present under
* {@value #CONFIG_DIR} in the bundle. This property can be used if a bundle needs to provide
* multiple core configurations
* @param override
* if files in the target directory should be overridden
* @throws IOException
* On any IO error while coping the configuration
* @throws IllegalStateException
* If the parsed coreDir does exist but is not a directory.
* @throws IllegalArgumentException
* if <code>null</code> is parsed as coreDir or if the parsed bundle does not contain the
* required information to set up an configuration or the parsed coreName is empty.
*/
public static void copyCore(Class<?> clazzInArchive, File coreDir, String coreName, boolean override) throws IOException, IllegalArgumentException, IllegalStateException {
if (coreDir == null) {
throw new IllegalArgumentException("The parsed core directory MUST NOT be NULL!");
}
if (coreDir.exists() && !coreDir.isDirectory()) {
throw new IllegalStateException("The parsed core directory " + coreDir.getAbsolutePath() + " extists but is not a directory!");
}
if (coreName != null && coreName.isEmpty()) {
throw new IllegalArgumentException("The parsed core name MUST NOT be empty (However NULL is supported)!");
}
String context = CORE_CONFIG_DIR + (coreName != null ? '/' + coreName : "");
File sourceRoot = getSource(clazzInArchive != null ? clazzInArchive : ConfigUtils.class);
if (sourceRoot.isFile()) {
ZipFile archive = new ZipFile(sourceRoot);
log.info(String.format("Copy core %s config from jar-file %s to %s (override=%s)", (coreName == null ? "" : coreName), sourceRoot.getName(), coreDir.getAbsolutePath(), override));
try {
for (@SuppressWarnings("unchecked") Enumeration<ZipArchiveEntry> entries = archive.getEntries(); entries.hasMoreElements(); ) {
ZipArchiveEntry entry = entries.nextElement();
if (entry.getName().startsWith(context)) {
copyResource(coreDir, archive, entry, context, override);
}
}
} finally {
// regardless what happens we need to close the archive!
ZipFile.closeQuietly(archive);
}
} else {
// load from file
File source = new File(sourceRoot, context);
if (source.exists() && source.isDirectory()) {
FileUtils.copyDirectory(source, coreDir);
} else {
throw new FileNotFoundException("The SolrIndex default config was not found in directory " + source.getAbsolutePath());
}
}
}
Aggregations