1. 程式人生 > >zip壓縮/解壓縮帶空資料夾的檔案

zip壓縮/解壓縮帶空資料夾的檔案

zip壓縮/解壓縮帶空資料夾的檔案

2011年07月21日 20:04:54 flex_work 閱讀數:7152 標籤: filestringbytebuffernullinclude 更多

個人分類: Flex And Java

       在EncryptZip的專案中對Zip的加密/解密沒有包含帶子資料夾的需求, 只有一層的關係. 

在此文中給出普通的zip壓縮/解壓縮的Java程式碼,  壓縮時遞迴壓縮檔案,包含檔案及檔案下的空資料夾. 若Flex端需要類似的功能, 可以參考本文中的zipFileWithTier 和unZipFileWithTier方法.

  程式碼如下:

        

 
  1. public class ZipFileWithTier {

  2.  
  3. private static final String zipPath = "C:\\temp\\Lemur\\empty.zip";

  4. private static final String unzipPath = "C:\\temp\\Lemur\\";

  5. private static final String srcFiles = "C:\\temp\\Lemur\\empty";

  6.  
  7. @Test

  8. public void zipFile(){

  9. File file = new File(zipPath);

  10. if(file.exists())

  11. file.delete();

  12. zipFileWithTier(srcFiles, zipPath);

  13. }

  14.  
  15. @Test

  16. public void upZipFile(){

  17. try {

  18. unzipFilesWithTier(readFileByte(zipPath), unzipPath + File.separator);

  19. } catch (IOException e) {

  20.  
  21. e.printStackTrace();

  22. }

  23. }

  24.  
  25. /*

  26. * Compress the specify file that contains sub-folders and store them to a zipfile.

  27. * @param srcFiles: the file will be compressed

  28. * @param zipPath: the location which stores the zipfile.

  29. */

  30. public static void zipFileWithTier(String srcFiles, String zipPath) {

  31. try {

  32.  
  33. FileOutputStream zipFile = new FileOutputStream(zipPath);

  34. BufferedOutputStream buffer = new BufferedOutputStream(zipFile);

  35. ZipOutputStream out = new ZipOutputStream(buffer);

  36. zipFiles(srcFiles, out, "");

  37. out.close();

  38. } catch (IOException e) {

  39. // TODO Auto-generated catch block

  40. e.printStackTrace();

  41. }

  42. }

  43.  
  44. /*

  45. * Recursive the specify file also contains the folder which may don't include any file.

  46. * @param filePath: compress file

  47. * @param ZipOutputStream: the zipfile's outputStream.

  48. * @param prefix: the prefix indicates the parent folder name of the file that makes the tier relation.

  49. */

  50. public static void zipFiles(String filePath, ZipOutputStream out, String prefix)

  51. throws IOException {

  52. File file = new File(filePath);

  53. if (file.isDirectory()) {

  54. if (file.listFiles().length == 0) {

  55. ZipEntry zipEntry = new ZipEntry(prefix + file.getName() + "/");

  56. out.putNextEntry(zipEntry);

  57. out.closeEntry();

  58. } else {

  59. prefix += file.getName() + File.separator;

  60. for (File f : file.listFiles())

  61. zipFiles(f.getAbsolutePath(), out, prefix);

  62. }

  63. } else {

  64. FileInputStream in = new FileInputStream(file);

  65. ZipEntry zipEntry = new ZipEntry(prefix + file.getName());

  66. out.putNextEntry(zipEntry);

  67. byte[] buf = new byte[1024];

  68. int len;

  69. while ((len = in.read(buf)) > 0) {

  70. out.write(buf, 0, len);

  71. }

  72. out.closeEntry();

  73. in.close();

  74. }

  75.  
  76. }

  77.  
  78. /*

  79. * Unzip the file also contains the folder which the listFiles's length is 0.

  80. * @param bytes: the content of the zipfile by byte array. *

  81. * @param prefix: the prefix is the root of the store path.

  82. * @IOExcetion: the ioexception during unzipFiles.

  83. */

  84. public static void unzipFilesWithTier(byte[] bytes, String prefix) throws IOException {

  85.  
  86. InputStream bais = new ByteArrayInputStream(bytes);

  87. ZipInputStream zin = new ZipInputStream(bais);

  88. ZipEntry ze;

  89. while ((ze = zin.getNextEntry()) != null) {

  90. if (ze.isDirectory()) {

  91. File file = new File(prefix + ze.getName());

  92. if (!file.exists())

  93. file.mkdirs();

  94. continue;

  95. }

  96. File file = new File(prefix + ze.getName());

  97. if (!file.getParentFile().exists())

  98. file.getParentFile().mkdirs();

  99. ByteArrayOutputStream toScan = new ByteArrayOutputStream();

  100. byte[] buf = new byte[1024];

  101. int len;

  102. while ((len = zin.read(buf)) > 0) {

  103. toScan.write(buf, 0, len);

  104. }

  105. byte[] fileOut = toScan.toByteArray();

  106. toScan.close();

  107. writeByteFile(fileOut, new File(prefix + ze.getName()));

  108. }

  109. zin.close();

  110. bais.close();

  111. }

  112.  
  113. public static byte[] readFileByte(String filename) throws IOException {

  114.  
  115. if (filename == null || filename.equals("")) {

  116. throw new NullPointerException("File is not exist!");

  117. }

  118. File file = new File(filename);

  119. long len = file.length();

  120. byte[] bytes = new byte[(int) len];

  121.  
  122. BufferedInputStream bufferedInputStream = new BufferedInputStream(

  123. new FileInputStream(file));

  124. int r = bufferedInputStream.read(bytes);

  125. if (r != len)

  126. throw new IOException("Read file failure!");

  127. bufferedInputStream.close();

  128.  
  129. return bytes;

  130.  
  131. }

  132.  
  133. public static String writeByteFile(byte[] bytes, File file) {

  134. FileOutputStream fos = null;

  135. try {

  136. fos = new FileOutputStream(file);

  137. fos.write(bytes);

  138. } catch (FileNotFoundException e) {

  139. e.printStackTrace();

  140.  
  141. } catch (IOException e) {

  142. e.printStackTrace();

  143. } finally {

  144. if (fos != null) {

  145. try {

  146. fos.close();

  147. } catch (IOException e) {

  148. e.printStackTrace();

  149. }

  150. }

  151. }

  152. return "success";

  153. }

  154.  
  155.  
  156.  
  157. }

 

 

(轉貼請註明出處) 

Author:David

   Mail:[email protected]

 

Python爬蟲全棧教學,零基礎教你成程式設計大神

零基礎學爬蟲,你要掌握學習那些技能?