use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project buck by facebook.
the class ZipRuleIntegrationTest method shouldZipSources.
@Test
public void shouldZipSources() throws IOException {
ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp);
workspace.setUp();
Path zip = workspace.buildAndReturnOutput("//example:ziptastic");
// Make sure we have the right files and attributes.
try (ZipFile zipFile = new ZipFile(zip.toFile())) {
ZipArchiveEntry cake = zipFile.getEntry("cake.txt");
assertThat(cake, Matchers.notNullValue());
assertFalse(cake.isUnixSymlink());
assertFalse(cake.isDirectory());
ZipArchiveEntry beans = zipFile.getEntry("beans/");
assertThat(beans, Matchers.notNullValue());
assertFalse(beans.isUnixSymlink());
assertTrue(beans.isDirectory());
ZipArchiveEntry cheesy = zipFile.getEntry("beans/cheesy.txt");
assertThat(cheesy, Matchers.notNullValue());
assertFalse(cheesy.isUnixSymlink());
assertFalse(cheesy.isDirectory());
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project buck by facebook.
the class ZipStepTest method willRecurseIntoSubdirectories.
@Test
public void willRecurseIntoSubdirectories() throws IOException {
Path parent = tmp.newFolder("zipstep");
Path out = parent.resolve("output.zip");
Path toZip = tmp.newFolder("zipdir");
Files.createFile(toZip.resolve("file1.txt"));
Files.createDirectories(toZip.resolve("child"));
Files.createFile(toZip.resolve("child/file2.txt"));
ZipStep step = new ZipStep(filesystem, Paths.get("zipstep/output.zip"), ImmutableSet.of(), false, ZipCompressionLevel.DEFAULT_COMPRESSION_LEVEL, Paths.get("zipdir"));
assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode());
// Make sure we have the right attributes.
try (ZipFile zip = new ZipFile(out.toFile())) {
ZipArchiveEntry entry = zip.getEntry("child/");
assertNotEquals(entry.getUnixMode() & MoreFiles.S_IFDIR, 0);
}
try (Zip zip = new Zip(out, false)) {
assertEquals(ImmutableSet.of("file1.txt", "child/file2.txt"), zip.getFileNames());
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry 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.ZipArchiveEntry 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.ZipArchiveEntry in project zm-mailbox by Zimbra.
the class ZipUtil method getZipEntryNames.
/**
* Traditional java.util.zip processing either assumes archives use UTF-8 for filenames or requires that
* you know up front what charset is used for filenames.
* This class uses the more versatile org.apache.commons.compress.archivers.zip package combined with
* language information to make a best guess at what the filenames might be.
*
*/
public static List<String> getZipEntryNames(InputStream inputStream, Locale locale) throws IOException {
List<String> zipEntryNames = Lists.newArrayList();
/*
* From http://commons.apache.org/proper/commons-compress/zip.html
* Traditionally the ZIP archive format uses CodePage 437 as encoding for file name, which is not sufficient for
* many international character sets. Over time different archivers have chosen different ways to work around
* the limitation - the java.util.zip packages simply uses UTF-8 as its encoding for example.
*
* For our purposes, CP437 has the advantage that all byte sequences are valid, so it works well as a final
* fallback charset to assume for the name.
*/
try (ZipArchiveInputStream zis = new ZipArchiveInputStream(inputStream, cp437charset.name(), false)) {
ZipArchiveEntry ze;
while ((ze = zis.getNextZipEntry()) != null) {
if (ze.isDirectory()) {
continue;
}
String entryName = bestGuessAtEntryName(ze, locale);
zipEntryNames.add(entryName);
}
}
return zipEntryNames;
}
Aggregations