如果我的應用程序想要以動態方式使用java壓縮結果文件(文件組),Java中有哪些可用選項? 當我瀏覽我有java.util.zip包使用,但有沒有其他方式,我可以用它來實現?使用Java壓縮和解壓縮文件夾和文件
6
A
回答
0
您可以使用JDK附帶的ZIP文件處理庫 也可以使用this教程。
15
public class FolderZiper {
public static void main(String[] a) throws Exception {
zipFolder("c:\\a", "c:\\a.zip");
}
static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
addFolderToZip("", srcFolder, zip);
zip.flush();
zip.close();
}
static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
throws Exception {
File folder = new File(srcFile);
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip);
} else {
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
}
static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
throws Exception {
File folder = new File(srcFolder);
for (String fileName : folder.list()) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
}
}
}
}
3
原始的Java實現已知有一些與文件編碼相關的錯誤。例如,它不能用變音符號正確處理文件名。
TrueZIP是我們在項目中使用的替代方案:https://truezip.dev.java.net/ 查看網站上的文檔。
0
的Java有一個java.util.zip.ZipInputStream中與此相伴,你可以使用ZipEntry的...像
public static void unZipIt(String zipFile, String outputFolder){
File folder = new File(zipFile);
List<String> files = listFilesForFolder(folder);
System.out.println("Size " + files.size());
byte[] buffer = new byte[1024];
try{
Iterator<String> iter = files.iterator();
while(iter.hasNext()){
String file = iter.next();
System.out.println("file name " + file);
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
ZipEntry ze = zis.getNextEntry();
while(ze!=null){
String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);
System.out.println("file unzip : "+ newFile.getAbsoluteFile());
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("Done");
}
}catch(IOException ex){
ex.printStackTrace();
}
}
相關問題
- 1. 壓縮和解壓縮文件和文件夾
- 2. 壓縮/解壓縮文件夾和文件
- 3. Node.js - 壓縮/解壓縮文件夾
- 4. Magick ++壓縮和解壓縮文件
- 5. 如何在Silverlight中壓縮和解壓縮文件夾及其子文件夾?
- 6. 在C#中壓縮和解壓縮文件夾#
- 7. Android - 解壓縮文件夾?
- 8. tomcat和解壓縮文件
- 9. Powershell,WMI和壓縮文件/文件夾
- 10. 如何使用rubyzip解壓縮壓縮文件夾
- 11. 使用zlib和cPickle將字典壓縮/解壓縮到文件
- 12. 通過C#使用cmd來壓縮和解壓縮文件
- 13. Java ZIP - 如何解壓縮文件夾?
- 14. java解壓縮文件
- 15. 解壓縮saz文件java
- 16. 壓縮和解壓縮與每個「壓縮,gzip,bzip2和zip」文件
- 17. 壓縮/壓縮文件夾,同時排除特定文件夾
- 18. 使用qt壓縮文件夾/文件
- 19. 在python中壓縮和解壓縮文本文件
- 20. 壓縮解壓縮的exe文件
- 21. 在yii中壓縮/解壓縮文件
- 22. 解壓縮(解壓縮)由.net壓縮(解壓縮)的文件.net system.io.compression.gzipstream
- 23. 解壓縮文件
- 24. 解壓縮文件
- 25. 列出文件,無需解壓縮和選擇性解壓縮
- 26. Mule esb文件壓縮/編碼和解壓縮/解碼
- 27. 如何使用Java將文件夾及其所有文件和子目錄壓縮/解壓縮?
- 28. 使用java解壓縮文件
- 29. Java LZW壓縮和解壓縮圖像
- 30. Java壓縮和解壓縮byte []塊
http://devharbor.blogspot.com/2009/01/create-zip -archive-in-java.html 和google – ant
感謝您的代碼 – gmhk