net.lingala.zip4j.model.enums.CompressionMethod Java Examples

The following examples show how to use net.lingala.zip4j.model.enums.CompressionMethod. 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: ExtractZipFileIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtractFilesThrowsExceptionForWrongPasswordForZipStandardAndStore() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.ZIP_STANDARD, null);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  zipFile.addFiles(FILES_TO_ADD, zipParameters);

  try {
    zipFile = new ZipFile(generatedZipFile, "WRONG_PASSWORD".toCharArray());
    zipFile.extractAll(outputFolder.getPath());
    fail("Should throw an exception");
  } catch (ZipException e) {
    assertThat(e).isNotNull();
    assertThat(e.getType()).isEqualTo(ZipException.Type.WRONG_PASSWORD);
  }
}
 
Example #2
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddFilesWhenFilesAlreadyExistsRemovesFiles() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  zipFile.addFiles(FILES_TO_ADD, zipParameters);

  char[] newPassword = "SOME_OTHER_PASSWORD".toCharArray();
  zipFile = new ZipFile(generatedZipFile, newPassword);
  zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);

  zipFile.addFiles(FILES_TO_ADD, zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, newPassword, outputFolder, FILES_TO_ADD.size());
  List<String> fileNames = FILES_TO_ADD.stream().map(File::getName).collect(Collectors.toList());
  verifyZipFileContainsFiles(generatedZipFile, fileNames, CompressionMethod.DEFLATE, EncryptionMethod.ZIP_STANDARD,
      null);
}
 
Example #3
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddFilesWithUtf8Characters() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.addFiles(asList(
      TestUtils.getTestFileFromResources("sample.pdf"),
      TestUtils.getTestFileFromResources("бореиская.txt"),
      TestUtils.getTestFileFromResources("zero_byte_file.txt"),
      TestUtils.getTestFileFromResources("sample_text1.txt"),
      TestUtils.getTestFileFromResources("가나다.abc")
  ), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 5);
  List<String> fileNamesThatShouldExistInZip = asList(
      "sample.pdf",
      "бореиская.txt",
      "zero_byte_file.txt",
      "sample_text1.txt",
      "가나다.abc"
  );
  verifyZipFileContainsFiles(generatedZipFile, fileNamesThatShouldExistInZip, CompressionMethod.DEFLATE,
      EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
}
 
Example #4
Source File: ZipOutputStreamIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testZipOutputStreamThrowsExceptionWhenEntrySizeNotSetForStoreCompression() throws IOException {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.STORE);

  expectedException.expect(IllegalArgumentException.class);
  expectedException.expectMessage("uncompressed size should be set for zip entries of compression type store");

  try(ZipOutputStream zos = initializeZipOutputStream(false, InternalZipConstants.CHARSET_UTF_8)) {
    for (File fileToAdd : FILES_TO_ADD) {
      zipParameters.setLastModifiedFileTime(fileToAdd.lastModified());
      zipParameters.setFileNameInZip(fileToAdd.getName());
      zos.putNextEntry(zipParameters);
    }
  }
}
 
Example #5
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddStreamToWithStoreCompressionAndWithoutEncryption() throws IOException {
  File fileToAdd = TestUtils.getTestFileFromResources("бореиская.txt");
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  zipParameters.setFileNameInZip(fileToAdd.getName());
  ZipFile zipFile = new ZipFile(generatedZipFile);
  InputStream inputStream = new FileInputStream(fileToAdd);

  zipFile.addStream(inputStream, zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 1);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("бореиская.txt"), CompressionMethod.STORE, null, null);

  zipFile = new ZipFile(generatedZipFile);
  byte[] generalPurposeBytes = zipFile.getFileHeaders().get(0).getGeneralPurposeFlag();
  // assert that extra data record is not present
  assertThat(BitUtils.isBitSet(generalPurposeBytes[0], 3)).isTrue();
}
 
Example #6
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddStreamToWithStoreCompressionAndZipStandardEncryption() throws IOException {
  File fileToAdd = TestUtils.getTestFileFromResources("sample_text_large.txt");
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.ZIP_STANDARD, null);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  zipParameters.setFileNameInZip(fileToAdd.getName());
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  InputStream inputStream = new FileInputStream(fileToAdd);

  zipFile.addStream(inputStream, zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 1);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("sample_text_large.txt"), CompressionMethod.STORE,
      EncryptionMethod.ZIP_STANDARD, null);

  zipFile = new ZipFile(generatedZipFile, PASSWORD);
  byte[] generalPurposeBytes = zipFile.getFileHeaders().get(0).getGeneralPurposeFlag();
  // assert that extra data record is not present
  assertThat(BitUtils.isBitSet(generalPurposeBytes[0], 3)).isTrue();
}
 
Example #7
Source File: AesCipherInputStream.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void verifyContent(byte[] storedMac) throws IOException {
  if (getLocalFileHeader().isDataDescriptorExists()
      && CompressionMethod.DEFLATE.equals(Zip4jUtil.getCompressionMethod(getLocalFileHeader()))) {
    // Skip content verification in case of Deflate compression and if data descriptor exists.
    // In this case, we do not know the exact size of compressed data before hand and it is possible that we read
    // and pass more than required data into inflater, thereby corrupting the aes mac bytes.
    // See usage of PushBackInputStream in the project for how this push back of data is done
    // Unfortunately, in this case we cannot perform a content verification and have to skip
    return;
  }

  byte[] calculatedMac = getDecrypter().getCalculatedAuthenticationBytes();
  byte[] first10BytesOfCalculatedMac = new byte[AES_AUTH_LENGTH];
  System.arraycopy(calculatedMac, 0, first10BytesOfCalculatedMac, 0, InternalZipConstants.AES_AUTH_LENGTH);

  if (!Arrays.equals(storedMac, first10BytesOfCalculatedMac)) {
    throw new IOException("Reached end of data for this entry, but aes verification failed");
  }
}
 
Example #8
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddStreamWithStoreCompressionAndDefaultCharset() throws IOException {
  String koreanFileName = "가나다.abc";
  File fileToAdd = TestUtils.getTestFileFromResources(koreanFileName);
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  zipParameters.setFileNameInZip(fileToAdd.getName());
  ZipFile zipFile = new ZipFile(generatedZipFile);
  InputStream inputStream = new FileInputStream(fileToAdd);

  zipFile.addStream(inputStream, zipParameters);

  byte[] generalPurposeBytes = zipFile.getFileHeaders().get(0).getGeneralPurposeFlag();
  // assert that extra data record is not present
  assertThat(BitUtils.isBitSet(generalPurposeBytes[1], 3)).isTrue();
  assertThat(zipFile.getFileHeaders().get(0).getFileName()).isEqualTo(koreanFileName);
}
 
Example #9
Source File: ZipVersionUtils.java    From zip4j with Apache License 2.0 6 votes vote down vote up
public static VersionNeededToExtract determineVersionNeededToExtract(ZipParameters zipParameters) {
  VersionNeededToExtract versionRequired = VersionNeededToExtract.DEFAULT;

  if (zipParameters.getCompressionMethod() == CompressionMethod.DEFLATE) {
    versionRequired = VersionNeededToExtract.DEFLATE_COMPRESSED;
  }

  if (zipParameters.getEntrySize() > InternalZipConstants.ZIP_64_SIZE_LIMIT) {
    versionRequired = VersionNeededToExtract.ZIP_64_FORMAT;
  }

  if (zipParameters.isEncryptFiles() && zipParameters.getEncryptionMethod().equals(EncryptionMethod.AES)) {
    versionRequired = VersionNeededToExtract.AES_ENCRYPTED;
  }

  return versionRequired;
}
 
Example #10
Source File: ZipInputStreamIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private File createZipFile(CompressionMethod compressionMethod, boolean encryptFiles,
                           EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength, char[] password,
                           AesVersion aesVersion)
    throws IOException {

  File outputFile = temporaryFolder.newFile("output.zip");
  deleteFileIfExists(outputFile);

  ZipFile zipFile = new ZipFile(outputFile, password);

  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(compressionMethod);
  zipParameters.setEncryptFiles(encryptFiles);
  zipParameters.setEncryptionMethod(encryptionMethod);
  zipParameters.setAesKeyStrength(aesKeyStrength);
  zipParameters.setAesVersion(aesVersion);

  zipFile.addFiles(AbstractIT.FILES_TO_ADD, zipParameters);

  return outputFile;
}
 
Example #11
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private FileHeader generateFileHeader(long lastModifiedFileTime) {
  FileHeader fileHeader = new FileHeader();
  fileHeader.setVersionNeededToExtract(20);
  fileHeader.setCompressionMethod(CompressionMethod.STORE);
  fileHeader.setLastModifiedTime(lastModifiedFileTime);
  fileHeader.setUncompressedSize(1000L);
  fileHeader.setFileName(FILE_NAME_IN_ZIP);
  fileHeader.setFileNameLength(FILE_NAME_IN_ZIP.getBytes(InternalZipConstants.CHARSET_UTF_8).length);
  fileHeader.setEncrypted(true);
  fileHeader.setEncryptionMethod(EncryptionMethod.AES);
  fileHeader.setCrc(1231231L);
  fileHeader.setCompressedSize(23523L);
  fileHeader.setGeneralPurposeFlag(new byte[] {2, 28});
  fileHeader.setDataDescriptorExists(true);
  fileHeader.setExtraFieldLength(190);
  return fileHeader;
}
 
Example #12
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFilesWhenZipFileDoesNotExistCreatesSuccessfully() throws IOException {
  ZipFile zipFile = new ZipFile(generatedZipFile);

  zipFile.addFiles(asList(
      TestUtils.getTestFileFromResources("file_PDF_1MB.pdf"),
      TestUtils.getTestFileFromResources("sample_text1.txt")
  ), new ZipParameters());

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 2);
  verifyZipFileContainsFiles(generatedZipFile, asList("file_PDF_1MB.pdf", "sample_text1.txt"),
      CompressionMethod.DEFLATE, null, null);
}
 
Example #13
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private LocalFileHeader createLocalFileHeader(String fileNameSuffix, long compressedSize, long uncompressedSize,
                                              boolean useUtf8) {
  LocalFileHeader localFileHeader = new LocalFileHeader();
  localFileHeader.setVersionNeededToExtract(VERSION_NEEDED_TO_EXTRACT);
  localFileHeader.setFileName(FILE_NAME_PREFIX + fileNameSuffix);
  localFileHeader.setCompressedSize(compressedSize);
  localFileHeader.setUncompressedSize(uncompressedSize);
  localFileHeader.setGeneralPurposeFlag(generateGeneralPurposeBytes(useUtf8));
  localFileHeader.setCompressionMethod(CompressionMethod.DEFLATE);
  localFileHeader.setLastModifiedTime(LAST_MODIFIED_FILE_TIME);
  return localFileHeader;
}
 
Example #14
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyAesExtraDataRecord(AESExtraDataRecord aesExtraDataRecord, AesKeyStrength aesKeyStrength,
                                      AesVersion aesVersion) {
  assertThat(aesExtraDataRecord).isNotNull();
  assertThat(aesExtraDataRecord.getAesKeyStrength()).isEqualTo(aesKeyStrength);
  assertThat(aesExtraDataRecord.getAesVersion()).isEqualTo(aesVersion);
  assertThat(aesExtraDataRecord.getCompressionMethod()).isEqualTo(CompressionMethod.DEFLATE);
  assertThat(aesExtraDataRecord.getVendorID()).isEqualTo("AE");
}
 
Example #15
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyFileIsOf(File generatedZipFile, String fileName, CompressionMethod compressionMethod,
                            EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength, AesVersion aesVersion)
    throws ZipException {

  List<FileHeader> fileHeaders = getFileHeaders(generatedZipFile);
  FileHeader fileHeader = getFileHeaderFrom(fileHeaders, fileName);

  if (encryptionMethod == null || encryptionMethod == EncryptionMethod.NONE) {
    assertThat(fileHeader.isEncrypted()).isFalse();
    assertThat(fileHeader.getEncryptionMethod()).isIn(null, EncryptionMethod.NONE);
  } else {
    verifyAllFilesAreOf(singletonList(fileHeader), compressionMethod, encryptionMethod, aesKeyStrength, aesVersion);
  }
}
 
Example #16
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyAllFilesAreOf(List<FileHeader> fileHeaders, CompressionMethod compressionMethod,
                                 EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength,
                                 AesVersion aesVersion) {
  for (FileHeader fileHeader : fileHeaders) {
    if (fileHeader.isDirectory()) {
      assertThat(fileHeader.getCompressionMethod()).isEqualTo(CompressionMethod.STORE);
      assertThat(fileHeader.isEncrypted()).isFalse();
      assertThat(fileHeader.getAesExtraDataRecord()).isNull();
      assertThat(fileHeader.getCrc()).isZero();
    } else {
      CompressionMethod shouldBeCompressionMethod = getShouldBeCompressionMethod(
          encryptionMethod == EncryptionMethod.AES, compressionMethod, fileHeader.getUncompressedSize());
      assertThat(fileHeader.getCompressionMethod()).isEqualTo(shouldBeCompressionMethod);

      if (encryptionMethod == null) {
        assertThat(fileHeader.getEncryptionMethod()).isEqualTo(EncryptionMethod.NONE);
      } else {
        assertThat(fileHeader.getEncryptionMethod()).isEqualTo(encryptionMethod);
      }

      if (encryptionMethod == EncryptionMethod.AES) {
        verifyAesExtraDataRecord(fileHeader.getAesExtraDataRecord(), aesKeyStrength, aesVersion);

        if (fileHeader.getAesExtraDataRecord().getAesVersion().equals(AesVersion.TWO)) {
          assertThat(fileHeader.getCrc()).isZero();
        } else {
          if (fileHeader.getCompressedSize() != 0) {
            assertThat(fileHeader.getCrc()).isNotZero();
          }
        }
      } else {
        assertThat(fileHeader.getAesExtraDataRecord()).isNull();
      }
    }
  }
}
 
Example #17
Source File: RenameFilesInZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenameWithMapWithAESEncryptionAndUtf8FileName() throws IOException {
  ZipFile zipFile = createZipFileWithFolder(CompressionMethod.DEFLATE, true, EncryptionMethod.AES);
  Map<String, String> fileNamesMap = new HashMap<>();
  fileNamesMap.put("test-files/가나다.abc", "test-files/üßööß.abc");
  fileNamesMap.put("test-files/öüäöäö/", "test-files/가나다/");

  zipFile.renameFiles(fileNamesMap);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 13, false);
  verifyFileNamesChanged(zipFile, fileNamesMap, false);
}
 
Example #18
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private CompressionMethod getShouldBeCompressionMethod(boolean isAesEncrypted, CompressionMethod compressionMethod,
                                                       long uncompressedSize) {
  if (isAesEncrypted) {
    return CompressionMethod.AES_INTERNAL_ONLY;
  }

  if (uncompressedSize == 0) {
    return CompressionMethod.STORE;
  }

  return compressionMethod;
}
 
Example #19
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFilesWithAes256EncryptionV1() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
  zipParameters.setAesVersion(AesVersion.ONE);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.addFiles(FILES_TO_ADD, zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, FILES_TO_ADD.size());
  List<String> fileNames = FILES_TO_ADD.stream().map(File::getName).collect(Collectors.toList());
  verifyZipFileContainsFiles(generatedZipFile, fileNames, CompressionMethod.DEFLATE, EncryptionMethod.AES,
      AesKeyStrength.KEY_STRENGTH_256, AesVersion.ONE);
}
 
Example #20
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddStreamToZipWithoutEncryptionForNewZipAddsSuccessfully() throws IOException {
  File fileToAdd = TestUtils.getTestFileFromResources("бореиская.txt");
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setFileNameInZip(fileToAdd.getName());
  ZipFile zipFile = new ZipFile(generatedZipFile);
  InputStream inputStream = new FileInputStream(fileToAdd);

  zipFile.addStream(inputStream, zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 1);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("бореиская.txt"), CompressionMethod.DEFLATE, null, null);
}
 
Example #21
Source File: AbstractAddFileToZipTask.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void addSymlinkToZip(File fileToAdd, ZipOutputStream zipOutputStream, ZipParameters zipParameters,
                             SplitOutputStream splitOutputStream) throws IOException {

  ZipParameters clonedZipParameters = new ZipParameters(zipParameters);
  clonedZipParameters.setFileNameInZip(replaceFileNameInZip(zipParameters.getFileNameInZip(), fileToAdd.getName()));
  clonedZipParameters.setEncryptFiles(false);
  clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);

  zipOutputStream.putNextEntry(clonedZipParameters);

  String symLinkTarget = FileUtils.readSymbolicLink(fileToAdd);
  zipOutputStream.write(symLinkTarget.getBytes());

  closeEntry(zipOutputStream, splitOutputStream, fileToAdd, true);
}
 
Example #22
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderForStoreWithoutEncryption() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.STORE);

  FileHeader fileHeader = fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);
  verifyFileHeader(fileHeader, zipParameters, false, 0, 10, false);
}
 
Example #23
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyAesExtraDataRecord(AESExtraDataRecord aesExtraDataRecord, AesKeyStrength aesKeyStrength,
                                      CompressionMethod compressionMethod, AesVersion aesVersion) {
  assertThat(aesExtraDataRecord).isNotNull();
  assertThat(aesExtraDataRecord.getSignature()).isEqualTo(HeaderSignature.AES_EXTRA_DATA_RECORD);
  assertThat(aesExtraDataRecord.getDataSize()).isEqualTo(7);
  assertThat(aesExtraDataRecord.getVendorID()).isEqualTo("AE");
  assertThat(aesExtraDataRecord.getCompressionMethod()).isEqualTo(compressionMethod);
  assertThat(aesExtraDataRecord.getAesVersion()).isEqualTo(aesVersion);
  assertThat(aesExtraDataRecord.getAesKeyStrength()).isEqualTo(aesKeyStrength);
}
 
Example #24
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFileAsStringWithZipParametersStoreAndStandardEncryption() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.ZIP_STANDARD, null);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.addFile(TestUtils.getTestFileFromResources("sample_text_large.txt").getPath(), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 1);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("sample_text_large.txt"), CompressionMethod.STORE,
      EncryptionMethod.ZIP_STANDARD, null);
}
 
Example #25
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFileWithoutZipParameterAddsAsDeflate() throws IOException {
  ZipFile zipFile = new ZipFile(generatedZipFile);
  zipFile.addFile(TestUtils.getTestFileFromResources("file_PDF_1MB.pdf"));

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 1);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("file_PDF_1MB.pdf"), CompressionMethod.DEFLATE, null,
      null);
}
 
Example #26
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFileWithZipParametersStoreAndStandardZip() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.ZIP_STANDARD, null);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.addFile(TestUtils.getTestFileFromResources("sample_text_large.txt"), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 1);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("sample_text_large.txt"), CompressionMethod.STORE,
      EncryptionMethod.ZIP_STANDARD, null);
}
 
Example #27
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFileWithZipParametersStoreAndAes128Encryption() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_128);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.addFile(TestUtils.getTestFileFromResources("sample_text_large.txt"), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 1);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("sample_text_large.txt"), CompressionMethod.STORE,
      EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_128);
  verifyZipVersions(zipFile.getFileHeaders().get(0), zipParameters);
}
 
Example #28
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFileRemovesExistingFileWithAesEncryption() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  List<File> filesToAdd = new ArrayList<>(FILES_TO_ADD);
  filesToAdd.add(1, TestUtils.getTestFileFromResources("file_PDF_1MB.pdf"));
  zipFile.addFiles(filesToAdd, zipParameters);

  zipFile.addFile(TestUtils.getTestFileFromResources("file_PDF_1MB.pdf"), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, FILES_TO_ADD.size()
      + 1);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("file_PDF_1MB.pdf"), CompressionMethod.DEFLATE,
      EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
}
 
Example #29
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyGeneralPurposeBytes(byte[] generalPurposeBytes, ZipParameters zipParameters) {
  assertThat(generalPurposeBytes).isNotNull();
  assertThat(generalPurposeBytes.length).isEqualTo(2);
  assertThat(isBitSet(generalPurposeBytes[0], 0)).isEqualTo(zipParameters.isEncryptFiles());
  if (zipParameters.getCompressionMethod() == CompressionMethod.DEFLATE) {
    verifyCompressionLevelGridForDeflate(zipParameters.getCompressionLevel(),
        generalPurposeBytes[0]);
  } else {
    assertThat(isBitSet(generalPurposeBytes[0], 1)).isFalse();
    assertThat(isBitSet(generalPurposeBytes[0], 2)).isFalse();
  }
  assertThat(isBitSet(generalPurposeBytes[0], 3)).isEqualTo(zipParameters.isWriteExtendedLocalFileHeader());
  assertThat(isBitSet(generalPurposeBytes[1], 3)).isTrue();
}
 
Example #30
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyCompressionMethod(FileHeader fileHeader, ZipParameters zipParameters) {
  if (fileHeader.isEncrypted() && fileHeader.getEncryptionMethod().equals(EncryptionMethod.AES)) {
    assertThat(fileHeader.getCompressionMethod()).isEqualTo(CompressionMethod.AES_INTERNAL_ONLY);
  } else {
    assertThat(fileHeader.getCompressionMethod()).isEqualTo(zipParameters.getCompressionMethod());
  }
}