Java Code Examples for net.lingala.zip4j.model.ZipParameters#setEntrySize()

The following examples show how to use net.lingala.zip4j.model.ZipParameters#setEntrySize() . 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: AbstractAddFileToZipTask.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private ZipParameters cloneAndAdjustZipParameters(ZipParameters zipParameters, File fileToAdd,
                                                  ProgressMonitor progressMonitor) throws IOException {
  ZipParameters clonedZipParameters = new ZipParameters(zipParameters);
  clonedZipParameters.setLastModifiedFileTime(javaToDosTime((fileToAdd.lastModified())));

  if (fileToAdd.isDirectory()) {
    clonedZipParameters.setEntrySize(0);
  } else {
    clonedZipParameters.setEntrySize(fileToAdd.length());
  }

  clonedZipParameters.setWriteExtendedLocalFileHeader(false);
  clonedZipParameters.setLastModifiedFileTime(fileToAdd.lastModified());

  if (!Zip4jUtil.isStringNotNullAndNotEmpty(zipParameters.getFileNameInZip())) {
    String relativeFileName = getRelativeFileName(fileToAdd, zipParameters);
    clonedZipParameters.setFileNameInZip(relativeFileName);
  }

  if (fileToAdd.isDirectory()) {
    clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);
    clonedZipParameters.setEncryptionMethod(EncryptionMethod.NONE);
    clonedZipParameters.setEncryptFiles(false);
  } else {
    if (clonedZipParameters.isEncryptFiles() && clonedZipParameters.getEncryptionMethod() == ZIP_STANDARD) {
      progressMonitor.setCurrentTask(CALCULATE_CRC);
      clonedZipParameters.setEntryCRC(computeFileCrc(fileToAdd, progressMonitor));
      progressMonitor.setCurrentTask(ADD_ENTRY);
    }

    if (fileToAdd.length() == 0) {
      clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);
    }
  }

  return clonedZipParameters;
}
 
Example 2
Source File: ZipFileZip64IT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testZip64WithSingleLargeZipEntry() throws IOException {
  long entrySize = InternalZipConstants.ZIP_64_SIZE_LIMIT + 1;

  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  zipParameters.setEntrySize(entrySize);

  createZip64FileWithEntries(1, entrySize, zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 1, false);
  verifyZip64HeadersPresent();

  cleanupOutputFolder();

  ZipFile zipFile = new ZipFile(generatedZipFile);
  zipFile.renameFile("FILE_0", "NEW_FILE_0");

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 1, false);
  verifyZip64HeadersPresent();
  HeaderVerifier.verifyFileHeadersExist(zipFile, Collections.singletonList("NEW_FILE_0"));
  HeaderVerifier.verifyFileHeadersDoesNotExist(zipFile, Collections.singletonList("FILE_0"));

  cleanupOutputFolder();

  zipFile = new ZipFile(generatedZipFile);
  zipFile.removeFile("NEW_FILE_0");

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 0, false);
  HeaderVerifier.verifyFileHeadersDoesNotExist(zipFile, Collections.singletonList("NEW_FILE_0"));
}
 
Example 3
Source File: ZipFileZip64IT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testZip64WithCentralDirectoryOffsetGreaterThanZip64Limit() throws IOException {
  long eachEntrySize = (InternalZipConstants.ZIP_64_SIZE_LIMIT / 2) + 100;

  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEntrySize(eachEntrySize);

  createZip64FileWithEntries(3, eachEntrySize, zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 3, false);
  verifyZip64HeadersPresent();

  cleanupOutputFolder();

  ZipFile zipFile = new ZipFile(generatedZipFile);
  Map<String, String> fileNamesMap = new HashMap<>();
  fileNamesMap.put("FILE_1", "NEW_FILE_1");
  fileNamesMap.put("FILE_2", "NEW_FILE_2");
  zipFile.renameFiles(fileNamesMap);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 3, false);
  verifyZip64HeadersPresent();
  HeaderVerifier.verifyFileHeadersExist(zipFile, fileNamesMap.values());
  HeaderVerifier.verifyFileHeadersDoesNotExist(zipFile, fileNamesMap.keySet());

  cleanupOutputFolder();

  zipFile = new ZipFile(generatedZipFile);
  List<String> filesToRemove = new ArrayList<>();
  filesToRemove.add("FILE_0");
  filesToRemove.add("NEW_FILE_2");
  zipFile.removeFiles(filesToRemove);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 1, false);
  HeaderVerifier.verifyFileHeadersDoesNotExist(zipFile, filesToRemove);
  HeaderVerifier.verifyFileHeadersExist(zipFile, Collections.singleton("NEW_FILE_1"));
}
 
Example 4
Source File: ZipFileZip64IT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testZip64WithNumberOfEntriesGreaterThan70k() throws IOException {
  long eachEntrySize = 100;

  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEntrySize(eachEntrySize);

  createZip64FileWithEntries(70000, eachEntrySize, zipParameters);

  ZipFile zipFile = new ZipFile(generatedZipFile);
  assertThat(zipFile.getFileHeaders()).hasSize(70000);
  verifyZip64HeadersPresent();

  zipFile = new ZipFile(generatedZipFile);
  Map<String, String> fileNamesMap = new HashMap<>();
  fileNamesMap.put("FILE_10", "NEW_FILE_10");
  fileNamesMap.put("FILE_20", "NEW_FILE_20");
  fileNamesMap.put("FILE_30", "NEW_FILE_30");
  zipFile.renameFiles(fileNamesMap);

  verifyZip64HeadersPresent();
  HeaderVerifier.verifyFileHeadersExist(zipFile, fileNamesMap.values());
  HeaderVerifier.verifyFileHeadersDoesNotExist(zipFile, fileNamesMap.keySet());

  zipFile = new ZipFile(generatedZipFile);
  List<String> filesToRemove = new ArrayList<>();
  filesToRemove.add("FILE_0");
  filesToRemove.add("NEW_FILE_10");
  filesToRemove.add("NEW_FILE_30");
  zipFile.removeFiles(filesToRemove);

  HeaderVerifier.verifyFileHeadersDoesNotExist(zipFile, filesToRemove);
}
 
Example 5
Source File: ZipOutputStreamIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void testZipOutputStream(CompressionMethod compressionMethod, boolean encrypt,
                                 EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength,
                                 AesVersion aesVersion, boolean setLastModifiedTime, List<File> filesToAdd, Charset charset)
    throws IOException {

  ZipParameters zipParameters = buildZipParameters(compressionMethod, encrypt, encryptionMethod, aesKeyStrength);
  zipParameters.setAesVersion(aesVersion);

  byte[] buff = new byte[4096];
  int readLen;

  try(ZipOutputStream zos = initializeZipOutputStream(encrypt, charset)) {
    for (File fileToAdd : filesToAdd) {

      if (zipParameters.getCompressionMethod() == CompressionMethod.STORE) {
        zipParameters.setEntrySize(fileToAdd.length());
      }

      if (setLastModifiedTime) {
        zipParameters.setLastModifiedFileTime(fileToAdd.lastModified());
      }
      zipParameters.setFileNameInZip(fileToAdd.getName());
      zos.putNextEntry(zipParameters);

      try(InputStream inputStream = new FileInputStream(fileToAdd)) {
        while ((readLen = inputStream.read(buff)) != -1) {
          zos.write(buff, 0, readLen);
        }
      }
      zos.closeEntry();
    }
  }
  verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, filesToAdd.size(), true, charset);
  verifyEntries();
}
 
Example 6
Source File: ZipVersionUtilsTest.java    From zip4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDetermineVersionNeededToExtractZip64() {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEntrySize(InternalZipConstants.ZIP_64_SIZE_LIMIT + 10);
  assertThat(ZipVersionUtils.determineVersionNeededToExtract(zipParameters)).isEqualTo(VersionNeededToExtract.ZIP_64_FORMAT);
}