use of com.google.devtools.build.zip.ZipReader in project bazel by bazelbuild.
the class ZipCombiner method addZip.
/**
* Adds the contents of a ZIP file to the combined ZIP file using the specified
* {@link ZipEntryFilter} to determine the appropriate action for each file.
*
* @param zipFile the ZIP file to add to the combined ZIP file
* @throws IOException if there is an error reading the ZIP file or writing entries to the
* combined ZIP file
*/
public void addZip(File zipFile) throws IOException {
try (ZipReader zip = new ZipReader(zipFile)) {
for (ZipFileEntry entry : zip.entries()) {
String filename = entry.getName();
EntryAction action = getAction(filename);
switch(action.getType()) {
case SKIP:
break;
case COPY:
case RENAME:
writeEntry(zip, entry, action);
break;
case MERGE:
entries.put(filename, null);
InputStream in = zip.getRawInputStream(entry);
if (entry.getMethod() == Compression.DEFLATED) {
in = new InflaterInputStream(in, getInflater());
}
action.getStrategy().merge(in, action.getMergeBuffer());
break;
}
}
}
}
use of com.google.devtools.build.zip.ZipReader in project bazel by bazelbuild.
the class ZipCombinerTest method testZipCombinerAgainstJavaUtil.
@Test
public void testZipCombinerAgainstJavaUtil() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (JarOutputStream jarOut = new JarOutputStream(out)) {
ZipEntry entry;
entry = new ZipEntry("META-INF/");
entry.setTime(ZipCombiner.DOS_EPOCH.getTime());
entry.setMethod(JarOutputStream.STORED);
entry.setSize(0);
entry.setCompressedSize(0);
entry.setCrc(0);
jarOut.putNextEntry(entry);
entry = new ZipEntry("META-INF/MANIFEST.MF");
entry.setTime(ZipCombiner.DOS_EPOCH.getTime());
entry.setMethod(JarOutputStream.DEFLATED);
jarOut.putNextEntry(entry);
jarOut.write(new byte[] { 1, 2, 3, 4 });
}
File javaFile = writeInputStreamToFile(new ByteArrayInputStream(out.toByteArray()));
out.reset();
try (ZipCombiner zipcombiner = new ZipCombiner(out)) {
zipcombiner.addDirectory("META-INF/", ZipCombiner.DOS_EPOCH, new ExtraData[] { new ExtraData((short) 0xCAFE, new byte[0]) });
zipcombiner.addFile("META-INF/MANIFEST.MF", ZipCombiner.DOS_EPOCH, new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }));
}
File zipCombinerFile = writeInputStreamToFile(new ByteArrayInputStream(out.toByteArray()));
byte[] zipCombinerRaw = out.toByteArray();
new ZipTester(zipCombinerRaw).validate();
assertZipFilesEquivalent(new ZipReader(zipCombinerFile), new ZipReader(javaFile));
}
use of com.google.devtools.build.zip.ZipReader in project bazel by bazelbuild.
the class ZipDecompressor method decompress.
/**
* This unzips the zip file to a sibling directory of {@link DecompressorDescriptor#archivePath}.
* The zip file is expected to have the WORKSPACE file at the top level, e.g.:
*
* <pre>
* $ unzip -lf some-repo.zip
* Archive: ../repo.zip
* Length Date Time Name
* --------- ---------- ----- ----
* 0 2014-11-20 15:50 WORKSPACE
* 0 2014-11-20 16:10 foo/
* 236 2014-11-20 15:52 foo/BUILD
* ...
* </pre>
*/
@Override
@Nullable
public Path decompress(DecompressorDescriptor descriptor) throws RepositoryFunctionException {
Path destinationDirectory = descriptor.archivePath().getParentDirectory();
Optional<String> prefix = descriptor.prefix();
boolean foundPrefix = false;
try (ZipReader reader = new ZipReader(descriptor.archivePath().getPathFile())) {
Collection<ZipFileEntry> entries = reader.entries();
for (ZipFileEntry entry : entries) {
StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
foundPrefix = foundPrefix || entryPath.foundPrefix();
if (entryPath.skip()) {
continue;
}
extractZipEntry(reader, entry, destinationDirectory, entryPath.getPathFragment());
}
} catch (IOException e) {
throw new RepositoryFunctionException(new IOException(String.format("Error extracting %s to %s: %s", descriptor.archivePath(), destinationDirectory, e.getMessage())), Transience.TRANSIENT);
}
if (prefix.isPresent() && !foundPrefix) {
throw new RepositoryFunctionException(new IOException("Prefix " + prefix.get() + " was given, but not found in the zip"), Transience.PERSISTENT);
}
return destinationDirectory;
}
Aggregations