Java Code Examples for org.apache.commons.compress.archivers.zip.ZipArchiveEntry#setSize()

The following examples show how to use org.apache.commons.compress.archivers.zip.ZipArchiveEntry#setSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DownloadService.java    From fredbet with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
byte[] compressToZipFile(List<BinaryImage> allImages) {
	try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
			ZipArchiveOutputStream zipOutput = new ZipArchiveOutputStream(byteOut);) {

		zipOutput.setEncoding("UTF-8");

		for (int i = 0; i < allImages.size(); i++) {
			BinaryImage image = allImages.get(i);
			String fileName = createEntryFileName(image, i + 1);
			ZipArchiveEntry entry = new ZipArchiveEntry(fileName);
			entry.setSize(image.getImageBinary().length);
			zipOutput.putArchiveEntry(entry);
			copyToOutputStream(zipOutput, image.getImageBinary());
			zipOutput.closeArchiveEntry();
		}
		zipOutput.close();
		return byteOut.toByteArray();
	} catch (Exception e) {
		LOG.error(e.getMessage(), e);
		return null;
	}
}
 
Example 2
Source File: ZipEdgeArchiveBuilder.java    From datacollector with Apache License 2.0 6 votes vote down vote up
protected void addArchiveEntry(
    ArchiveOutputStream archiveOutput,
    Object fileContent,
    String pipelineId,
    String fileName
) throws IOException {
  File pipelineFile = File.createTempFile(pipelineId, fileName);
  FileOutputStream pipelineOutputStream = new FileOutputStream(pipelineFile);
  ObjectMapperFactory.get().writeValue(pipelineOutputStream, fileContent);
  pipelineOutputStream.flush();
  pipelineOutputStream.close();
  ZipArchiveEntry archiveEntry = new ZipArchiveEntry(
      pipelineFile,
      DATA_PIPELINES_FOLDER + pipelineId + "/" + fileName
  );
  archiveEntry.setSize(pipelineFile.length());
  archiveOutput.putArchiveEntry(archiveEntry);
  IOUtils.copy(new FileInputStream(pipelineFile), archiveOutput);
  archiveOutput.closeArchiveEntry();
}
 
Example 3
Source File: ScatterZipOutputStream.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
void writeTo(ZipArchiveOutputStream target) throws IOException {
    entryStore.closeForWriting();
    backingStore.closeForWriting();
    String line;

    try (InputStream stream = backingStore.getInputStream();
         BufferedReader reader = new BufferedReader(new InputStreamReader(entryStore.getInputStream(), StandardCharsets.UTF_8))) {
        while ((line = reader.readLine()) != null) {
            String[] values = line.split(",");
            if (values.length != 5)
                throw new IOException("Failed to read temporary zip entry.");

            ZipArchiveEntry entry = new ZipArchiveEntry(values[0]);
            entry.setMethod(Integer.parseInt(values[1]));
            entry.setCrc(Long.parseLong(values[2]));
            entry.setCompressedSize(Long.parseLong(values[3]));
            entry.setSize(Long.parseLong(values[4]));

            try (BoundedInputStream rawStream = new BoundedInputStream(stream, entry.getCompressedSize())) {
                target.addRawArchiveEntry(entry, rawStream);
            }
        }
    } catch (NumberFormatException e) {
        throw new IOException("Failed to read temporary zip entry.", e);
    }
}
 
Example 4
Source File: MCRZipServlet.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void sendCompressedFile(MCRPath file, BasicFileAttributes attrs, ZipArchiveOutputStream container)
    throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(getFilename(file));
    entry.setTime(attrs.lastModifiedTime().toMillis());
    entry.setSize(attrs.size());
    container.putArchiveEntry(entry);
    try {
        Files.copy(file, container);
    } finally {
        container.closeArchiveEntry();
    }
}
 
Example 5
Source File: MCRZipServlet.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void sendMetadataCompressed(String fileName, byte[] content, long lastModified,
    ZipArchiveOutputStream container) throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(fileName);
    entry.setSize(content.length);
    entry.setTime(lastModified);
    container.putArchiveEntry(entry);
    container.write(content);
    container.closeArchiveEntry();
}
 
Example 6
Source File: LogArchiver.java    From cuba with Apache License 2.0 5 votes vote down vote up
private static ArchiveEntry newTailArchive(String name, byte[] tail) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(name);
    zipEntry.setSize(tail.length);
    zipEntry.setCompressedSize(zipEntry.getSize());
    CRC32 crc32 = new CRC32();
    crc32.update(tail);
    zipEntry.setCrc(crc32.getValue());
    return zipEntry;
}
 
Example 7
Source File: LogArchiver.java    From cuba with Apache License 2.0 5 votes vote down vote up
private static ArchiveEntry newArchive(File file) throws IOException {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(file.getName());
    zipEntry.setSize(file.length());
    zipEntry.setCompressedSize(zipEntry.getSize());
    zipEntry.setCrc(FileUtils.checksumCRC32(file));
    return zipEntry;
}
 
Example 8
Source File: FoldersServiceBean.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected ArchiveEntry newStoredEntry(String name, byte[] data) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(name);
    zipEntry.setSize(data.length);
    zipEntry.setCompressedSize(zipEntry.getSize());
    CRC32 crc32 = new CRC32();
    crc32.update(data);
    zipEntry.setCrc(crc32.getValue());
    return zipEntry;
}
 
Example 9
Source File: EntityImportExport.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected ArchiveEntry newStoredEntry(String name, byte[] data) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(name);
    zipEntry.setSize(data.length);
    zipEntry.setCompressedSize(zipEntry.getSize());
    CRC32 crc32 = new CRC32();
    crc32.update(data);
    zipEntry.setCrc(crc32.getValue());
    return zipEntry;
}
 
Example 10
Source File: TestUtils.java    From coroutines with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void writeJarEntry(JarArchiveOutputStream jaos, String name, byte[] data) throws IOException {
    ZipArchiveEntry entry = new JarArchiveEntry(name);
    entry.setSize(data.length);
    jaos.putArchiveEntry(entry);
    jaos.write(data);
    jaos.closeArchiveEntry();
}
 
Example 11
Source File: UnzipTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractZipFilePreservesExecutePermissionsAndModificationTime()
    throws InterruptedException, IOException {

  // getFakeTime returs time with some non-zero millis. By doing division and multiplication by
  // 1000 we get rid of that.
  long time = ZipConstants.getFakeTime() / 1000 * 1000;

  // Create a simple zip archive using apache's commons-compress to store executable info.
  try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
    ZipArchiveEntry entry = new ZipArchiveEntry("test.exe");
    entry.setUnixMode(
        (int) MorePosixFilePermissions.toMode(PosixFilePermissions.fromString("r-x------")));
    entry.setSize(DUMMY_FILE_CONTENTS.length);
    entry.setMethod(ZipEntry.STORED);
    entry.setTime(time);
    zip.putArchiveEntry(entry);
    zip.write(DUMMY_FILE_CONTENTS);
    zip.closeArchiveEntry();
  }

  // Now run `Unzip.extractZipFile` on our test zip and verify that the file is executable.
  Path extractFolder = tmpFolder.newFolder();
  ImmutableList<Path> result =
      ArchiveFormat.ZIP
          .getUnarchiver()
          .extractArchive(
              new DefaultProjectFilesystemFactory(),
              zipFile.toAbsolutePath(),
              extractFolder.toAbsolutePath(),
              ExistingFileMode.OVERWRITE);
  Path exe = extractFolder.toAbsolutePath().resolve("test.exe");
  assertTrue(Files.exists(exe));
  assertThat(Files.getLastModifiedTime(exe).toMillis(), Matchers.equalTo(time));
  assertTrue(Files.isExecutable(exe));
  assertEquals(ImmutableList.of(extractFolder.resolve("test.exe")), result);
}
 
Example 12
Source File: UnzipTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractSymlink() throws InterruptedException, IOException {
  assumeThat(Platform.detect(), Matchers.is(Matchers.not(Platform.WINDOWS)));

  // Create a simple zip archive using apache's commons-compress to store executable info.
  try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
    ZipArchiveEntry entry = new ZipArchiveEntry("link.txt");
    entry.setUnixMode((int) MostFiles.S_IFLNK);
    String target = "target.txt";
    entry.setSize(target.getBytes(Charsets.UTF_8).length);
    entry.setMethod(ZipEntry.STORED);
    zip.putArchiveEntry(entry);
    zip.write(target.getBytes(Charsets.UTF_8));
    zip.closeArchiveEntry();
  }

  Path extractFolder = tmpFolder.newFolder();

  ArchiveFormat.ZIP
      .getUnarchiver()
      .extractArchive(
          new DefaultProjectFilesystemFactory(),
          zipFile.toAbsolutePath(),
          extractFolder.toAbsolutePath(),
          ExistingFileMode.OVERWRITE);
  Path link = extractFolder.toAbsolutePath().resolve("link.txt");
  assertTrue(Files.isSymbolicLink(link));
  assertThat(Files.readSymbolicLink(link).toString(), Matchers.equalTo("target.txt"));
}
 
Example 13
Source File: UnzipTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractBrokenSymlinkWithOwnerExecutePermissions()
    throws InterruptedException, IOException {
  assumeThat(Platform.detect(), Matchers.is(Matchers.not(Platform.WINDOWS)));

  // Create a simple zip archive using apache's commons-compress to store executable info.
  try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
    ZipArchiveEntry entry = new ZipArchiveEntry("link.txt");
    entry.setUnixMode((int) MostFiles.S_IFLNK);
    String target = "target.txt";
    entry.setSize(target.getBytes(Charsets.UTF_8).length);
    entry.setMethod(ZipEntry.STORED);

    // Mark the file as being executable.
    Set<PosixFilePermission> filePermissions = ImmutableSet.of(PosixFilePermission.OWNER_EXECUTE);

    long externalAttributes =
        entry.getExternalAttributes() + (MorePosixFilePermissions.toMode(filePermissions) << 16);
    entry.setExternalAttributes(externalAttributes);

    zip.putArchiveEntry(entry);
    zip.write(target.getBytes(Charsets.UTF_8));
    zip.closeArchiveEntry();
  }

  Path extractFolder = tmpFolder.newFolder();

  ArchiveFormat.ZIP
      .getUnarchiver()
      .extractArchive(
          new DefaultProjectFilesystemFactory(),
          zipFile.toAbsolutePath(),
          extractFolder.toAbsolutePath(),
          ExistingFileMode.OVERWRITE);
  Path link = extractFolder.toAbsolutePath().resolve("link.txt");
  assertTrue(Files.isSymbolicLink(link));
  assertThat(Files.readSymbolicLink(link).toString(), Matchers.equalTo("target.txt"));
}
 
Example 14
Source File: ZipStripper.java    From reproducible-build-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void strip(File in, File out) throws IOException
{
    try (final ZipFile zip = new ZipFile(in);
         final ZipArchiveOutputStream zout = new ZipArchiveOutputStream(out))
    {
        final List<String> sortedNames = sortEntriesByName(zip.getEntries());
        for (String name : sortedNames)
        {
            final ZipArchiveEntry entry = zip.getEntry(name);
            // Strip Zip entry
            final ZipArchiveEntry strippedEntry = filterZipEntry(entry);
            // Fix external file attributes if required
            if (in.getName().endsWith(".jar") || in.getName().endsWith(".war"))
            {
                fixAttributes(strippedEntry);
            }
            // Strip file if required
            final Stripper stripper = getSubFilter(name);
            if (stripper != null)
            {
                // Unzip entry to temp file
                final File tmp = File.createTempFile("tmp", null);
                tmp.deleteOnExit();
                Files.copy(zip.getInputStream(entry), tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);
                final File tmp2 = File.createTempFile("tmp", null);
                tmp2.deleteOnExit();
                stripper.strip(tmp, tmp2);
                final byte[] fileContent = Files.readAllBytes(tmp2.toPath());
                strippedEntry.setSize(fileContent.length);
                zout.putArchiveEntry(strippedEntry);
                zout.write(fileContent);
                zout.closeArchiveEntry();
            }
            else
            {
                // Copy the Zip entry as-is
                zout.addRawArchiveEntry(strippedEntry, zip.getRawInputStream(entry));
            }
        }
    }
}
 
Example 15
Source File: UnzipTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void testStripsPrefixAndIgnoresSiblings() throws IOException {
  byte[] bazDotSh = "echo \"baz.sh\"\n".getBytes(Charsets.UTF_8);
  try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
    zip.putArchiveEntry(new ZipArchiveEntry("foo"));
    zip.closeArchiveEntry();
    zip.putArchiveEntry(new ZipArchiveEntry("foo/bar/baz.txt"));
    zip.write(DUMMY_FILE_CONTENTS, 0, DUMMY_FILE_CONTENTS.length);
    zip.closeArchiveEntry();

    ZipArchiveEntry exeEntry = new ZipArchiveEntry("foo/bar/baz.sh");
    exeEntry.setUnixMode(
        (int) MorePosixFilePermissions.toMode(PosixFilePermissions.fromString("r-x------")));
    exeEntry.setMethod(ZipEntry.STORED);
    exeEntry.setSize(bazDotSh.length);
    zip.putArchiveEntry(exeEntry);
    zip.write(bazDotSh);

    zip.closeArchiveEntry();
    zip.putArchiveEntry(new ZipArchiveEntry("sibling"));
    zip.closeArchiveEntry();
    zip.putArchiveEntry(new ZipArchiveEntry("sibling/some/dir/and/file.txt"));
    zip.write(DUMMY_FILE_CONTENTS, 0, DUMMY_FILE_CONTENTS.length);
    zip.closeArchiveEntry();
  }

  Path extractFolder = Paths.get("output_dir", "nested");

  ProjectFilesystem filesystem =
      TestProjectFilesystems.createProjectFilesystem(tmpFolder.getRoot());

  ArchiveFormat.ZIP
      .getUnarchiver()
      .extractArchive(
          zipFile,
          filesystem,
          extractFolder,
          Optional.of(Paths.get("foo")),
          ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES);

  assertFalse(filesystem.isDirectory(extractFolder.resolve("sibling")));
  assertFalse(filesystem.isDirectory(extractFolder.resolve("foo")));
  assertFalse(filesystem.isDirectory(extractFolder.resolve("some")));

  Path bazDotTxtPath = extractFolder.resolve("bar").resolve("baz.txt");
  Path bazDotShPath = extractFolder.resolve("bar").resolve("baz.sh");

  assertTrue(filesystem.isDirectory(extractFolder.resolve("bar")));
  assertTrue(filesystem.isFile(bazDotTxtPath));
  assertTrue(filesystem.isFile(bazDotShPath));
  assertTrue(filesystem.isExecutable(bazDotShPath));
  assertEquals(new String(bazDotSh), filesystem.readFileIfItExists(bazDotShPath).get());
  assertEquals(
      new String(DUMMY_FILE_CONTENTS), filesystem.readFileIfItExists(bazDotTxtPath).get());
}