net.lingala.zip4j.util.Zip4jConstants Java Examples
The following examples show how to use
net.lingala.zip4j.util.Zip4jConstants.
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: UnzipEngine.java From AndroidZip with Apache License 2.0 | 7 votes |
public void checkCRC() throws ZipException { if (fileHeader != null) { if (fileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) { if (decrypter != null && decrypter instanceof AESDecrypter) { byte[] tmpMacBytes = ((AESDecrypter)decrypter).getCalculatedAuthenticationBytes(); byte[] storedMac = ((AESDecrypter)decrypter).getStoredMac(); byte[] calculatedMac = new byte[InternalZipConstants.AES_AUTH_LENGTH]; if (calculatedMac == null || storedMac == null) { throw new ZipException("CRC (MAC) check failed for " + fileHeader.getFileName()); } System.arraycopy(tmpMacBytes, 0, calculatedMac, 0, InternalZipConstants.AES_AUTH_LENGTH); if (!Arrays.equals(calculatedMac, storedMac)) { throw new ZipException("invalid CRC (MAC) for file: " + fileHeader.getFileName()); } } } else { long calculatedCRC = crc.getValue() & 0xffffffffL; if (calculatedCRC != fileHeader.getCrc32()) { String errMsg = "invalid CRC for file: " + fileHeader.getFileName(); if (localFileHeader.isEncrypted() && localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) { errMsg += " - Wrong Password?"; } throw new ZipException(errMsg); } } } }
Example #2
Source File: DesaElementVisitor.java From proarc with GNU General Public License v3.0 | 6 votes |
/** * 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 #3
Source File: ZipArchive.java From Mzip-Android with Apache License 2.0 | 6 votes |
public static void zip(String targetPath, String destinationFilePath, String password) { try { ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); if (password.length() > 0) { parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); parameters.setPassword(password); } ZipFile zipFile = new ZipFile(destinationFilePath); File targetFile = new File(targetPath); if (targetFile.isFile()) { zipFile.addFile(targetFile, parameters); } else if (targetFile.isDirectory()) { zipFile.addFolder(targetFile, parameters); } } catch (Exception e) { e.printStackTrace(); } }
Example #4
Source File: ZipUtil.java From MonitorClient with Apache License 2.0 | 6 votes |
/** * 添加文件夹到zip中 * @data 2017年4月18日 * @param inPath * @param storagePath * @param outPath * @param password * @return */ public static boolean addFoldInZip(String inPath,String storagePath,String outPath,String password) { try { ZipFile zipFile = new ZipFile(outPath); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); 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.addFolder(inPath, parameters); return true; } catch (ZipException e) { e.printStackTrace(); return false; } }
Example #5
Source File: CompressionHandler.java From butterfly with MIT License | 6 votes |
void compress(TransformationResult transformationResult) { File inputFile = transformationResult.getTransformedApplicationDir().getAbsoluteFile(); File compressedFile = new File(transformationResult.getTransformedApplicationDir().getAbsolutePath() + ".zip"); logger.info("Compressing transformed application"); try { ZipFile zipFile = new ZipFile(compressedFile); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); zipFile.addFolder(inputFile, parameters); FileUtils.deleteDirectory(transformationResult.getTransformedApplicationDir()); logger.info("Transformed application has been compressed to {}", compressedFile.getAbsoluteFile()); } catch (Exception e) { logger.error("An exception happened when compressing transformed application", e); } }
Example #6
Source File: ZipUtil.java From document-management-software with GNU Lesser General Public License v3.0 | 6 votes |
public static void addEntry(File zip, String entry, InputStream content) { try { // read war.zip and write to ZipFile zipFile = new ZipFile(zip); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // this would be the name of the file for this entry in the zip file parameters.setFileNameInZip(entry); // we set this flag to true. If this flag is true, Zip4j identifies // that // the data will not be from a file but directly from a stream parameters.setSourceExternalStream(true); // Creates a new entry in the zip file and adds the content to the // zip // file zipFile.addStream(content, parameters); } catch (Exception e) { logError(e.getMessage()); } }
Example #7
Source File: ZipUtil.java From MonitorClient with Apache License 2.0 | 6 votes |
/** * 添加文件到zip中指定的文件夹中 * @param inPath * @param storagePath * @param outPath * @param password * @return */ public static boolean addFileInZip(String inPath,String storagePath,String outPath,String password) { try { ZipFile zipFile = new ZipFile(outPath); File inFile = new File(inPath); 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.addFile(inFile, parameters); return true; } catch (ZipException e) { e.printStackTrace(); return false; } }
Example #8
Source File: CipherOutputStream.java From AndroidZip with Apache License 2.0 | 6 votes |
private AESExtraDataRecord generateAESExtraDataRecord(ZipParameters parameters) throws ZipException { if (parameters == null) { throw new ZipException("zip parameters are null, cannot generate AES Extra Data record"); } AESExtraDataRecord aesDataRecord = new AESExtraDataRecord(); aesDataRecord.setSignature(InternalZipConstants.AESSIG); aesDataRecord.setDataSize(7); aesDataRecord.setVendorID("AE"); // Always set the version number to 2 as we do not store CRC for any AES encrypted files // only MAC is stored and as per the specification, if version number is 2, then MAC is read // and CRC is ignored aesDataRecord.setVersionNumber(2); if (parameters.getAesKeyStrength() == Zip4jConstants.AES_STRENGTH_128) { aesDataRecord.setAesStrength(Zip4jConstants.AES_STRENGTH_128); } else if (parameters.getAesKeyStrength() == Zip4jConstants.AES_STRENGTH_256) { aesDataRecord.setAesStrength(Zip4jConstants.AES_STRENGTH_256); } else { throw new ZipException("invalid AES key strength, cannot generate AES Extra data record"); } aesDataRecord.setCompressionMethod(parameters.getCompressionMethod()); return aesDataRecord; }
Example #9
Source File: CipherOutputStream.java From AndroidZip with Apache License 2.0 | 6 votes |
private int[] generateGeneralPurposeBitArray(boolean isEncrpyted, int compressionMethod) { int[] generalPurposeBits = new int[8]; if (isEncrpyted) { generalPurposeBits[0] = 1; } else { generalPurposeBits[0] = 0; } if (compressionMethod == Zip4jConstants.COMP_DEFLATE) { // Have to set flags for deflate } else { generalPurposeBits[1] = 0; generalPurposeBits[2] = 0; } generalPurposeBits[3] = 1; return generalPurposeBits; }
Example #10
Source File: ZipUtil.java From MonitorClient with Apache License 2.0 | 6 votes |
/** * 添加多个文件到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 #11
Source File: CipherOutputStream.java From AndroidZip with Apache License 2.0 | 6 votes |
private void initEncrypter() throws ZipException { if (!zipParameters.isEncryptFiles()) { encrypter = null; return; } switch (zipParameters.getEncryptionMethod()) { case Zip4jConstants.ENC_METHOD_STANDARD: // Since we do not know the crc here, we use the modification time for encrypting. encrypter = new StandardEncrypter(zipParameters.getPassword(), (localFileHeader.getLastModFileTime() & 0x0000ffff) << 16); break; case Zip4jConstants.ENC_METHOD_AES: encrypter = new AESEncrpyter(zipParameters.getPassword(), zipParameters.getAesKeyStrength()); break; default: throw new ZipException("invalid encprytion method"); } }
Example #12
Source File: HeaderReader.java From AndroidZip with Apache License 2.0 | 6 votes |
/** * Reads AES Extra Data Record and saves it in the {@link LocalFileHeader} * @param localFileHeader * @throws ZipException */ private void readAndSaveAESExtraDataRecord(LocalFileHeader localFileHeader) throws ZipException { if (localFileHeader == null) { throw new ZipException("file header is null in reading Zip64 Extended Info"); } if (localFileHeader.getExtraDataRecords() == null || localFileHeader.getExtraDataRecords().size() <= 0) { return; } AESExtraDataRecord aesExtraDataRecord = readAESExtraDataRecord(localFileHeader.getExtraDataRecords()); if (aesExtraDataRecord != null) { localFileHeader.setAesExtraDataRecord(aesExtraDataRecord); localFileHeader.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); } }
Example #13
Source File: HeaderReader.java From AndroidZip with Apache License 2.0 | 6 votes |
/** * Reads AES Extra Data Record and saves it in the {@link FileHeader} * @param fileHeader * @throws ZipException */ private void readAndSaveAESExtraDataRecord(FileHeader fileHeader) throws ZipException { if (fileHeader == null) { throw new ZipException("file header is null in reading Zip64 Extended Info"); } if (fileHeader.getExtraDataRecords() == null || fileHeader.getExtraDataRecords().size() <= 0) { return; } AESExtraDataRecord aesExtraDataRecord = readAESExtraDataRecord(fileHeader.getExtraDataRecords()); if (aesExtraDataRecord != null) { fileHeader.setAesExtraDataRecord(aesExtraDataRecord); fileHeader.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); } }
Example #14
Source File: BackupAppsTasker.java From KAM with GNU General Public License v3.0 | 6 votes |
private void withData(AppsModel model, ApplicationInfo packageInfo, File file, FileUtil fileUtil, int count) throws Exception { File dataFolder = new File(packageInfo.dataDir); File dataFile = new File(fileUtil.getBaseFolderName() + model.getPackageName()); boolean z = RootManager.getInstance().copyFile(dataFolder.getPath(), fileUtil.getBaseFolderName()); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST); if (z) { if (dataFile.exists()) { zipData.addFolder(dataFile, parameters); FileUtils.deleteDirectory(dataFile); } } ProgressModel progressModel = new ProgressModel(); progressModel.setProgress(count); progressModel.setFileName(model.getAppName()); publishProgress(progressModel); }
Example #15
Source File: Zip4j.java From maven-framework-project with MIT License | 6 votes |
/** * 示例1 创建压缩包添加文件到压缩包中(未设置加密) */ @Test public void example1(){ try { ZipFile zipFile = new ZipFile("src/main/resources/AddFilesDeflateComp.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); // set compression method to deflate compression parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); zipFile.addFiles(filesToAdd, parameters); CompressUtil.printZipInfo("src/main/resources/AddFilesDeflateComp.zip"); } catch (ZipException e) { e.printStackTrace(); } }
Example #16
Source File: AESEncrpyter.java From AndroidZip with Apache License 2.0 | 6 votes |
public AESEncrpyter(char[] password, int keyStrength) throws ZipException { if (password == null || password.length == 0) { throw new ZipException("input password is empty or null in AES encrypter constructor"); } if (keyStrength != Zip4jConstants.AES_STRENGTH_128 && keyStrength != Zip4jConstants.AES_STRENGTH_256) { throw new ZipException("Invalid key strength in AES encrypter constructor"); } this.password = password; this.keyStrength = keyStrength; this.finished = false; counterBlock = new byte[InternalZipConstants.AES_BLOCK_SIZE]; iv = new byte[InternalZipConstants.AES_BLOCK_SIZE]; init(); }
Example #17
Source File: Zip4j.java From maven-framework-project with MIT License | 6 votes |
/** * 示例2 创建压缩包添加文件到指定目录中进行压缩 */ @Test public void example2(){ try { ZipFile zipFile = new ZipFile("src/main/resources/AddFilesDeflateComp.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); parameters.setRootFolderInZip("target/"); zipFile.addFiles(filesToAdd, parameters); } catch (ZipException e) { e.printStackTrace(); } }
Example #18
Source File: Zip4j.java From maven-framework-project with MIT License | 6 votes |
/** * 示例3 添加文件夹到压缩包中 */ @Test public void example3(){ try { ZipFile zipFile = new ZipFile("src/main/resources/AddFolder.zip"); String folderToAdd = "src/main/resources"; ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); zipFile.addFolder(folderToAdd, parameters); } catch (ZipException e) { e.printStackTrace(); } }
Example #19
Source File: Zip4j.java From maven-framework-project with MIT License | 6 votes |
/** * 示例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 #20
Source File: CejshBuilder.java From proarc with GNU General Public License v3.0 | 5 votes |
void writeZip(File pkgFile, File packageFolder) throws ZipException { ZipFile zipFile = new ZipFile(pkgFile); ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); zipParameters.setIncludeRootFolder(false); zipParameters.setDefaultFolderPath(packageFolder.getAbsolutePath()); zipFile.addFiles(listZipFiles(packageFolder), zipParameters); }
Example #21
Source File: CompressUtil.java From maven-framework-project with MIT License | 5 votes |
/** * 使用给定密码压缩指定文件或文件夹到指定位置. * <p> * dest可传最终压缩文件存放的绝对路径,也可以传存放目录,也可以传null或者"".<br /> * 如果传null或者""则将压缩文件存放在当前目录,即跟源文件同目录,压缩文件名取源文件名,以.zip为后缀;<br /> * 如果以路径分隔符(File.separator)结尾,则视为目录,压缩文件名取源文件名,以.zip为后缀,否则视为文件名. * * @param src * 要压缩的文件或文件夹路径 * @param dest * 压缩文件存放路径 * @param isCreateDir * 是否在压缩文件里创建目录,仅在压缩文件为目录时有效.<br /> * 如果为false,将直接压缩目录下文件到压缩文件. * @param passwd * 压缩使用的密码 * @return 最终的压缩文件存放的绝对路径,如果为null则说明压缩失败. */ public static String zip(String src, String dest, boolean isCreateDir, String passwd) { File srcFile = new File(src); dest = buildDestinationZipFilePath(srcFile, dest); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式 parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别 if (!StringUtils.isEmpty(passwd)) { parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式 parameters.setPassword(passwd.toCharArray()); } try { ZipFile zipFile = new ZipFile(dest); if (srcFile.isDirectory()) { // 如果不创建目录的话,将直接把给定目录下的文件压缩到压缩文件,即没有目录结构 if (!isCreateDir) { File[] subFiles = srcFile.listFiles(); ArrayList<File> temp = new ArrayList<File>(); Collections.addAll(temp, subFiles); zipFile.addFiles(temp, parameters); return dest; } zipFile.addFolder(srcFile, parameters); } else { zipFile.addFile(srcFile, parameters); } return dest; } catch (ZipException e) { e.printStackTrace(); } return null; }
Example #22
Source File: PdcaReportExcelCommand.java From bamboobsc with Apache License 2.0 | 5 votes |
private String createZipFile(List<SysUploadVO> uploads) throws Exception { ZipFile zip = new ZipFile( Constants.getWorkTmpDir() + SimpleUtils.getUUIDStr() + ".zip" ); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); for (SysUploadVO reportUpload : uploads) { zip.addFile(UploadSupportUtils.getRealFile(reportUpload.getOid()), parameters); } return UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, zip.getFile(), "pdca-report.zip"); }
Example #23
Source File: RNZipArchiveModule.java From react-native-zip-archive with MIT License | 5 votes |
@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 #24
Source File: FileIO.java From PHONK with GNU General Public License v3.0 | 5 votes |
static public void zipFolder(String src, String dst) throws Exception { File f = new File(dst); //make dirs if necessary f.getParentFile().mkdirs(); ZipFile zipfile = new ZipFile(f.getAbsolutePath()); ZipParameters parameters = new ZipParameters(); parameters.setIncludeRootFolder(false); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); zipfile.addFolder(src, parameters); }
Example #25
Source File: Zip4j.java From maven-framework-project with MIT License | 5 votes |
/** * 示例4 创建加密压缩包 */ @Test public void example4(){ try { ZipFile zipFile = new ZipFile("src/main/resources/AddFilesWithAESZipEncryption.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); parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); parameters.setPassword("123456"); zipFile.addFiles(filesToAdd, parameters); } catch (ZipException e) { e.printStackTrace(); } }
Example #26
Source File: ZipUtilities.java From openemm with GNU Affero General Public License v3.0 | 5 votes |
public static void compressToEncryptedZipFile(File destinationZipFile, File fileToZip, String fileNameInZip, String zipPassword) throws Exception { net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(destinationZipFile); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); if (StringUtils.isNotBlank(fileNameInZip)) { parameters.setFileNameInZip(fileNameInZip); } parameters.setPassword(zipPassword.toCharArray()); zipFile.addFile(fileToZip, parameters); }
Example #27
Source File: RxZipTool.java From RxTools-master with Apache License 2.0 | 5 votes |
public static String zipEncrypt(String src, String dest, boolean isCreateDir, String passwd) { File srcFile = new File(src); dest = buildDestinationZipFilePath(srcFile, dest); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式 parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别 if (!RxDataTool.isNullString(passwd)) { parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式 parameters.setPassword(passwd.toCharArray()); } try { net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(dest); if (srcFile.isDirectory()) { // 如果不创建目录的话,将直接把给定目录下的文件压缩到压缩文件,即没有目录结构 if (!isCreateDir) { File [] subFiles = srcFile.listFiles(); ArrayList<File> temp = new ArrayList<File>(); Collections.addAll(temp, subFiles); zipFile.addFiles(temp, parameters); return dest; } zipFile.addFolder(srcFile, parameters); } else { zipFile.addFile(srcFile, parameters); } return dest; } catch (ZipException e) { e.printStackTrace(); } return null; }
Example #28
Source File: ZipEngine.java From AndroidZip with Apache License 2.0 | 5 votes |
private void checkParameters(ZipParameters parameters) throws ZipException { if (parameters == null) { throw new ZipException("cannot validate zip parameters"); } if ((parameters.getCompressionMethod() != Zip4jConstants.COMP_STORE) && parameters.getCompressionMethod() != Zip4jConstants.COMP_DEFLATE) { throw new ZipException("unsupported compression type"); } if (parameters.getCompressionMethod() == Zip4jConstants.COMP_DEFLATE) { if (parameters.getCompressionLevel() < 0 && parameters.getCompressionLevel() > 9) { throw new ZipException("invalid compression level. compression level dor deflate should be in the range of 0-9"); } } if (parameters.isEncryptFiles()) { if (parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_STANDARD && parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_AES) { throw new ZipException("unsupported encryption method"); } if (parameters.getPassword() == null || parameters.getPassword().length <= 0) { throw new ZipException("input password is empty or null"); } } else { parameters.setAesKeyStrength(-1); parameters.setEncryptionMethod(-1); } }
Example #29
Source File: ZipEngine.java From AndroidZip with Apache License 2.0 | 5 votes |
private long calculateTotalWork(ArrayList fileList, ZipParameters parameters) throws ZipException { if (fileList == null) { throw new ZipException("file list is null, cannot calculate total work"); } long totalWork = 0; for (int i = 0; i < fileList.size(); i++) { if(fileList.get(i) instanceof File) { if (((File)fileList.get(i)).exists()) { if (parameters.isEncryptFiles() && parameters.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) { totalWork += (Zip4jUtil.getFileLengh((File)fileList.get(i)) * 2); } else { totalWork += Zip4jUtil.getFileLengh((File)fileList.get(i)); } if (zipModel.getCentralDirectory() != null && zipModel.getCentralDirectory().getFileHeaders() != null && zipModel.getCentralDirectory().getFileHeaders().size() > 0) { String relativeFileName = Zip4jUtil.getRelativeFileName( ((File)fileList.get(i)).getAbsolutePath(), parameters.getRootFolderInZip(), parameters.getDefaultFolderPath()); FileHeader fileHeader = Zip4jUtil.getFileHeader(zipModel, relativeFileName); if (fileHeader != null) { totalWork += (Zip4jUtil.getFileLengh(new File(zipModel.getZipFile())) - fileHeader.getCompressedSize()); } } } } } return totalWork; }
Example #30
Source File: UnzipEngine.java From AndroidZip with Apache License 2.0 | 5 votes |
private void initDecrypter(RandomAccessFile raf) throws ZipException { if (localFileHeader == null) { throw new ZipException("local file header is null, cannot init decrypter"); } if (localFileHeader.isEncrypted()) { if (localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) { decrypter = new StandardDecrypter(fileHeader, getStandardDecrypterHeaderBytes(raf)); } else if (localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) { decrypter = new AESDecrypter(localFileHeader, getAESSalt(raf), getAESPasswordVerifier(raf)); } else { throw new ZipException("unsupported encryption method"); } } }