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

The following examples show how to use net.lingala.zip4j.model.ZipParameters#setCompressionMethod() . 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: ZipUtil.java    From MonitorClient with Apache License 2.0 6 votes vote down vote up
/**
 * 添加多个文件到zip中指定的文件夹中
 * @param inPath
 * @param storagePath
 * @param outPath
 * @param password
 * @return
 */
public static boolean addFilesInZip(ArrayList<File> inFiles,String storagePath,String outPath,String password) {
	try {
		ArrayList filesToAdd = new ArrayList();  
		ZipFile zipFile = new ZipFile(outPath);
		ZipParameters parameters = new ZipParameters();
		parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
		parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 
		parameters.setRootFolderInZip(storagePath);  
		if(password!=null&&!password.equals("")){
			parameters.setEncryptFiles(true);  
			parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);  
			parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);  
			parameters.setPassword(password); 
		}
		zipFile.addFiles(inFiles, parameters);
		return true;
	} catch (ZipException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 2
Source File: Zip4j.java    From maven-framework-project with MIT License 6 votes vote down vote up
/**
 * 示例5 创建分卷压缩包
 */
@Test
public void example5(){
	try {
         
           ZipFile zipFile = new ZipFile("src/main/resources/CreateSplitZipFile.zip");

           ArrayList<File> filesToAdd = new ArrayList<File>();
           filesToAdd.add(new File("src/main/resources/sample.txt"));
           filesToAdd.add(new File("src/main/resources/zip4j.txt"));
           filesToAdd.add(new File("src/main/resources/zip4j-1.3.2.jar"));
            
           ZipParameters parameters = new ZipParameters();
           parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            
           parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            
           zipFile.createZipFile(filesToAdd, parameters, true, 65536);
       } catch (ZipException e) {
           e.printStackTrace();
       }
}
 
Example 3
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddStreamWithStoreCompressionAndCharset() 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.setCharset(CHARSET_CP_949);
  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)).isFalse();
  assertThat(zipFile.getFileHeaders().get(0).getFileName()).isEqualTo(koreanFileName);
}
 
Example 4
Source File: ZipManager.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
/**
 * 压缩多个文件
 *
 * @param list                被压缩的文件集合
 * @param destinationFilePath 压缩后生成的文件路径
 * @param password            压缩 密码
 * @param callback            回调
 */
public static void zip(ArrayList<File> list, String destinationFilePath, String password, final IZipCallback callback) {
    if (list == null || list.size() == 0 || !Zip4jUtil.isStringNotNullAndNotEmpty(destinationFilePath)) {
        if (callback != null) callback.onFinish(false);
        return;
    }
    ZipLog.debug("zip: list=" + list.toString() + " , destinationFilePath=" + destinationFilePath + " , password=" + password);
    try {
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(8);
        parameters.setCompressionLevel(5);
        if (password.length() > 0) {
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(99);
            parameters.setAesKeyStrength(3);
            parameters.setPassword(password);
        }
        ZipFile zipFile = new ZipFile(destinationFilePath);
        zipFile.setRunInThread(true);
        zipFile.addFiles(list, parameters);
        timerMsg(callback, zipFile);
    } catch (Exception e) {
        if (callback != null) callback.onFinish(false);
        ZipLog.debug("zip: Exception=" + e.getMessage());
    }
}
 
Example 5
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 6
Source File: DesaElementVisitor.java    From proarc with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Archives the list of files to a zip archive
 *
 * @param zipFileName
 * @param fileList
 * @param desaElement
 * @throws MetsExportException
 */
private void zip(String zipFileName, ArrayList<File> fileList, IDesaElement desaElement) throws MetsExportException {
    try {
        File file = new File(zipFileName);
        if (file.exists()) {
            file.delete();
            file = null;
            LOG.log(Level.FINE, "File:" + zipFileName + " exists, so it was deleted");
        }
        ZipFile zipFile = new ZipFile(zipFileName);
        ZipParameters zip4jZipParameters = new ZipParameters();
        zip4jZipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        zip4jZipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
        zipFile.addFiles(fileList, zip4jZipParameters);
        LOG.log(Level.FINE, "Zip archive created:" + zipFileName + " for " + desaElement.getElementType());
    } catch (ZipException e) {
        throw new MetsExportException(desaElement.getOriginalPid(), "Unable to create a zip file:" + zipFileName, false, e);
    }

}
 
Example 7
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFileRemovesExistingFileNoEncryptionSingleFileInZip() throws IOException {
  ZipFile zipFile = new ZipFile(generatedZipFile);
  zipFile.addFile(TestUtils.getTestFileFromResources("sample_text_large.txt"));

  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  zipFile.addFile(TestUtils.getTestFileFromResources("sample_text_large.txt"), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 1);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("sample_text_large.txt"), CompressionMethod.STORE,
      null, null);
}
 
Example 8
Source File: CollectEarthUtils.java    From collect-earth with MIT License 5 votes vote down vote up
public static void addFolderToZip(ZipFile zipFile, File folderToCompress )
		throws ZipException {
	ZipParameters zipParameters = new ZipParameters();
	// COMP_DEFLATE is for compression
	zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);
	// DEFLATE_LEVEL_ULTRA = maximum compression
	zipParameters.setCompressionLevel(CompressionLevel.ULTRA);

	if( folderToCompress.exists() && folderToCompress.isDirectory() ){
		zipFile.addFolder(folderToCompress, zipParameters);
	}
}
 
Example 9
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFileAsStringWithZipParametersStoreAndStandardEncryptionAndCharsetCp949() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.ZIP_STANDARD, null);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.setCharset(CHARSET_CP_949);
  String koreanFileName = "가나다.abc";
  zipFile.addFile(TestUtils.getTestFileFromResources(koreanFileName).getPath(), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 1, true, CHARSET_CP_949);
  assertThat(zipFile.getFileHeaders().get(0).getFileName()).isEqualTo(koreanFileName);
  verifyZipVersions(zipFile.getFileHeaders().get(0), zipParameters);
}
 
Example 10
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 11
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFolderWithStoreAndAes128() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_128);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.addFolder(TestUtils.getTestFileFromResources(""), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 13);
  List<FileHeader> fileHeaders = getFileHeaders(generatedZipFile);
  verifyAllFilesInZipContainsPath(fileHeaders, "test-files/");
  verifyFoldersInZip(fileHeaders, generatedZipFile, PASSWORD);
}
 
Example 12
Source File: ExtractZipFileIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractAllStoreAndAes128EncryptionExtractsSuccessfully() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_128);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  zipFile.addFiles(FILES_TO_ADD, zipParameters);

  zipFile.extractAll(outputFolder.getPath());

  ZipFileVerifier.verifyFolderContentsSameAsSourceFiles(outputFolder);
  verifyNumberOfFilesInOutputFolder(outputFolder, 3);
}
 
Example 13
Source File: RNZipArchiveModule.java    From react-native-zip-archive with MIT License 5 votes vote down vote up
@ReactMethod
public void zip(String fileOrDirectory, String destDirectory, Promise promise) {
  try{

    ZipParameters parameters = new ZipParameters();
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

    processZip(fileOrDirectory, destDirectory, parameters, promise);

  } catch (Exception ex) {
    promise.reject(null, ex.getMessage());
    return;
  }
}
 
Example 14
Source File: CreateZipFileIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateSplitZipFileStoreAndWithAesEncryptionKeyStrength128() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES,
      AesKeyStrength.KEY_STRENGTH_128);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);

  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  List<File> filesToAdd = new ArrayList<>(FILES_TO_ADD);
  filesToAdd.add(TestUtils.getTestFileFromResources("file_PDF_1MB.pdf"));
  zipFile.createSplitZipFile(filesToAdd, zipParameters, true, InternalZipConstants.MIN_SPLIT_LENGTH);

  verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, FILES_TO_ADD.size() + 1);
  verifySplitZip(generatedZipFile, 18, InternalZipConstants.MIN_SPLIT_LENGTH);
}
 
Example 15
Source File: CreateZipFileIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateSplitZipFileStoreAndWithAesEncryptionKeyStrength256() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES,
      AesKeyStrength.KEY_STRENGTH_256);
  zipParameters.setCompressionMethod(CompressionMethod.STORE);

  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  zipFile.createSplitZipFile(FILES_TO_ADD, zipParameters, true, InternalZipConstants.MIN_SPLIT_LENGTH);

  verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, FILES_TO_ADD.size());
  verifySplitZip(generatedZipFile, 2, InternalZipConstants.MIN_SPLIT_LENGTH);
}
 
Example 16
Source File: CreateZipFileIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateSplitZipFileStoreAndWithoutEncryption() throws IOException {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.STORE);

  ZipFile zipFile = new ZipFile(generatedZipFile);
  zipFile.createSplitZipFile(FILES_TO_ADD, zipParameters, true, InternalZipConstants.MIN_SPLIT_LENGTH);

  verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, FILES_TO_ADD.size());
  verifySplitZip(generatedZipFile, 2, InternalZipConstants.MIN_SPLIT_LENGTH);
}
 
Example 17
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 18
Source File: ZipVersionUtilsTest.java    From zip4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDetermineVersionNeededToExtractDefalte() {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);
  assertThat(ZipVersionUtils.determineVersionNeededToExtract(zipParameters)).isEqualTo(VersionNeededToExtract.DEFLATE_COMPRESSED);
}
 
Example 19
Source File: ZipVersionUtilsTest.java    From zip4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDetermineVersionNeededToExtractDefault() {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  assertThat(ZipVersionUtils.determineVersionNeededToExtract(zipParameters)).isEqualTo(VersionNeededToExtract.DEFAULT);
}
 
Example 20
Source File: RNZipArchiveModule.java    From react-native-zip-archive with MIT License 4 votes vote down vote up
@ReactMethod
public void zipWithPassword(String fileOrDirectory, String destDirectory, String password,
    String encryptionMethod, Promise promise) {
  try{

    ZipParameters parameters = new ZipParameters();
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

    String encParts[] = encryptionMethod.split("-");

    if (password != null && !password.isEmpty()) {
      parameters.setEncryptFiles(true);
      if (encParts[0].equals("AES")) {
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
        if (encParts[1].equals("128")) {
          parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_128);
        } else if (encParts[1].equals("256")) {
          parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
        } else {
          parameters.setAesKeyStrength(Zip4jConstants.ENC_METHOD_STANDARD);
        }
      } else if (encryptionMethod.equals("STANDARD")) {
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
        Log.d(TAG, "Standard Encryption");
      } else {
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
        Log.d(TAG, "Encryption type not supported default to Standard Encryption");
      }
      parameters.setPassword(password);
    } else {
      promise.reject(null, "Password is empty");
    }

    processZip(fileOrDirectory, destDirectory, parameters, promise);

  } catch (Exception ex) {
    promise.reject(null, ex.getMessage());
    return;
  }

}