Java Zip实用类

x33g5p2x  于2022-10-06 转载在 Java  
字(4.8k)|赞(0)|评价(0)|浏览(614)

在这篇文章中,我们将讨论几个常用的Java Zip工具方法。 

我们还可以向你展示Zip实用方法的JUnit测试案例样本。 

ZipUtils类包含以下通用方法。

  1. zipFiles(List files, OutputStream outputStream) - 将一个文件集合压缩到一个目标压缩输出流。
  2. unZipFiles(File zipFile, File outputFolder) - 解压缩一个压缩文件到一个输出文件夹。
  3. zipFiles(List files, File zipFile) - 将一个文件集合压缩到一个目标压缩文件。
  4. unZipFiles(InputStream inputStream, File outputFolder) - 将一个压缩文件从输入流中解压到一个输出文件夹。

Java Zip实用程序类 - ZipUtils

注意,ZipUtils.java包含一个第三方库--apache-commons-io.

  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.util.List;
  11. import java.util.zip.ZipEntry;
  12. import java.util.zip.ZipInputStream;
  13. import java.util.zip.ZipOutputStream;
  14. import org.apache.commons.io.FileUtils;
  15. import org.apache.commons.io.IOUtils;
  16. import org.apache.commons.lang3.StringUtils;
  17. /**
  18. * Utility for zipping files.
  19. * @author javaguides.net
  20. */
  21. public class ZipUtils {
  22. /**
  23. * Zips a collection of files to a destination zip output stream.
  24. *
  25. * @param files A collection of files and directories
  26. * @param outputStream The output stream of the destination zip file
  27. * @throws FileNotFoundException
  28. * @throws IOException
  29. */
  30. public static void zipFiles(List<File> files, OutputStream outputStream) throws IOException {
  31. ZipOutputStream zos = new ZipOutputStream(outputStream);
  32. for (File file : files) {
  33. if (file.isDirectory()) { //if it's a folder
  34. addFolderToZip("", file, zos);
  35. } else {
  36. addFileToZip("", file, zos);
  37. }
  38. }
  39. zos.finish();
  40. }
  41. /**
  42. * Adds a directory to the current zip
  43. *
  44. * @param path the path of the parent folder in the zip
  45. * @param folder the directory to be added
  46. * @param zos the current zip output stream
  47. * @throws FileNotFoundException
  48. * @throws IOException
  49. */
  50. private static void addFolderToZip(String path, File folder, ZipOutputStream zos) throws IOException {
  51. String currentPath = StringUtils.isNotEmpty(path)? path + "/" + folder.getName(): folder.getName();
  52. for (File file : folder.listFiles()) {
  53. if (file.isDirectory()) {
  54. addFolderToZip(currentPath, file, zos);
  55. } else {
  56. addFileToZip(currentPath, file, zos);
  57. }
  58. }
  59. }
  60. /**
  61. * Adds a file to the current zip output stream
  62. *
  63. * @param path the path of the parent folder in the zip
  64. * @param file the file to be added
  65. * @param zos the current zip output stream
  66. * @throws FileNotFoundException
  67. * @throws IOException
  68. */
  69. private static void addFileToZip(String path, File file, ZipOutputStream zos) throws IOException {
  70. String currentPath = StringUtils.isNotEmpty(path)? path + "/" + file.getName(): file.getName();
  71. zos.putNextEntry(new ZipEntry(currentPath));
  72. InputStream is = new BufferedInputStream(new FileInputStream(file));
  73. try {
  74. IOUtils.copy(is, zos);
  75. } finally {
  76. IOUtils.closeQuietly(is);
  77. }
  78. zos.closeEntry();
  79. }
  80. /**
  81. * Zips a collection of files to a destination zip file.
  82. *
  83. * @param files A collection of files and directories
  84. * @param zipFile The path of the destination zip file
  85. * @throws FileNotFoundException
  86. * @throws IOException
  87. */
  88. public static void zipFiles(List<File> files, File zipFile) throws IOException {
  89. OutputStream os = new BufferedOutputStream(new FileOutputStream(zipFile));
  90. try {
  91. zipFiles(files, os);
  92. } finally {
  93. IOUtils.closeQuietly(os);
  94. }
  95. }
  96. /**
  97. * Unzips a zip from an input stream into an output folder.
  98. *
  99. * @param inputStream the zip input stream
  100. * @param outputFolder the output folder where the files
  101. * @throws IOException
  102. */
  103. public static void unZipFiles(InputStream inputStream, File outputFolder) throws IOException {
  104. ZipInputStream zis = new ZipInputStream(inputStream);
  105. ZipEntry ze = zis.getNextEntry();
  106. while (ze != null) {
  107. File file = new File(outputFolder, ze.getName());
  108. OutputStream os = new BufferedOutputStream(FileUtils.openOutputStream(file));
  109. try {
  110. IOUtils.copy(zis, os);
  111. } finally {
  112. IOUtils.closeQuietly(os);
  113. }
  114. zis.closeEntry();
  115. ze = zis.getNextEntry();
  116. }
  117. }
  118. /**
  119. * Unzips a zip file into an output folder.
  120. *
  121. * @param zipFile the zip file
  122. * @param outputFolder the output folder where the files
  123. * @throws IOException
  124. */
  125. public static void unZipFiles(File zipFile, File outputFolder) throws IOException {
  126. InputStream is = new BufferedInputStream(new FileInputStream(zipFile));
  127. try {
  128. unZipFiles(is, outputFolder);
  129. } finally {
  130. IOUtils.closeQuietly(is);
  131. }
  132. }
  133. }

ZipUtilsTest

  1. package com.javaguides.javaio.utility;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.junit.Test;
  8. public class ZipUtilsTest {
  9. @Test
  10. public void zipFilesTest() throws IOException {
  11. FileOutputStream fos = new FileOutputStream("C:/Project_Work/samples/src_sample1.zip");
  12. File file = new File("C:\\Project_Work\\samples\\outputzip\\sample.txt");
  13. File file1 = new File("C:\\Project_Work\\samples\\outputzip\\sample1.txt");
  14. File file2 = new File("C:\\Project_Work\\samples\\outputzip\\sample2.txt");
  15. List<File> files = new ArrayList<>();
  16. files.add(file);
  17. files.add(file1);
  18. files.add(file2);
  19. ZipUtils.zipFiles(files, fos);
  20. }
  21. @Test
  22. public void unZipFilesTest() throws IOException {
  23. File zipFile = new File("C:/Project_Work/samples/src_sample1.zip");
  24. File unZipOutputFolder = new File("C:/Project_Work/samples/dest_folder");
  25. ZipUtils.unZipFiles(zipFile, unZipOutputFolder);
  26. }
  27. }

相关文章

最新文章

更多