Để sử dụng trược tiên bạn vào trang web của Zip4J để download thư việc JAR của nó về add vào project để sử dụng. |
Java Function Compress File or Folder.
public static void compress(String folderName, String zipFileName, String password) {
// Initiate ZipFile object with the path/name of the zip file.
File ff = new File(zipFileName);
if (ff.exists()) {
ff.delete();
//ff.deleteOnExit();
}
System.out.println("Folder Name: " + folderName);
System.out.println("Zip Folder:" + zipFileName);
ZipFile zipFile;
try {
zipFile = new ZipFile(zipFileName);
// Initiate Zip Parameters which define various properties such
// as compression method, etc.
ZipParameters parameters = new ZipParameters();
// set compression method to store compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
// Set the compression level
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
if (password != null && !"".endsWith(password)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
parameters.setPassword(password);
} else {
parameters.setEncryptFiles(false);
parameters.setEncryptionMethod(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
}
/**
* whether the root folder need to zip
*/
parameters.setIncludeRootFolder(true);
// Add folder to the zip file
File fFolder = new File(folderName);
if (fFolder.isDirectory()) {
System.out.println(fFolder + " is directory!");
zipFile.addFolder(fFolder, parameters);
} else {
System.out.println(fFolder + " is file!");
//fFolder.mkdir();
zipFile.addFile(fFolder, parameters);
}
} catch (ZipException ex) {
JOptionPane.showMessageDialog(null,ex.getMessage(),"Error warning",JOptionPane.ERROR_MESSAGE);
}
}
// Initiate ZipFile object with the path/name of the zip file.
File ff = new File(zipFileName);
if (ff.exists()) {
ff.delete();
//ff.deleteOnExit();
}
System.out.println("Folder Name: " + folderName);
System.out.println("Zip Folder:" + zipFileName);
ZipFile zipFile;
try {
zipFile = new ZipFile(zipFileName);
// Initiate Zip Parameters which define various properties such
// as compression method, etc.
ZipParameters parameters = new ZipParameters();
// set compression method to store compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
// Set the compression level
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
if (password != null && !"".endsWith(password)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
parameters.setPassword(password);
} else {
parameters.setEncryptFiles(false);
parameters.setEncryptionMethod(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
}
/**
* whether the root folder need to zip
*/
parameters.setIncludeRootFolder(true);
// Add folder to the zip file
File fFolder = new File(folderName);
if (fFolder.isDirectory()) {
System.out.println(fFolder + " is directory!");
zipFile.addFolder(fFolder, parameters);
} else {
System.out.println(fFolder + " is file!");
//fFolder.mkdir();
zipFile.addFile(fFolder, parameters);
}
} catch (ZipException ex) {
JOptionPane.showMessageDialog(null,ex.getMessage(),"Error warning",JOptionPane.ERROR_MESSAGE);
}
}
Related | Bài viết liên quan:
- Hàm Java giải nén tập tin Zip có hoặc không có password. | Java function decompress zip have password(be or not) using Zip4J.
- Làm thế nào để nén(zip) một file bằng lớp ZipOutputStream class in java? (How to zip a single file using ZipOutputStream class in java?)
- Làm thế sử dụng lớp ZipInputStream nào để giải nén một tập tin trong java? (How to unzip file using class ZipInputStream in Java?)
No comments:
Post a Comment