2017年4月25日 星期二

zip4j 自定義OutputStream

  1. public class ZipUtil{
  2. /**
  3. * 由於有zipModel的原因
  4. * 故不建議使用static
  5. * */
  6.  
  7. private ZipModel zipModel = new ZipModel();
  8.  
  9. /*
  10. public static void main(String[] args) {
  11. ZipUtil util = new ZipUtil();
  12. try {
  13. util.doZipEnc();
  14. System.out.println("成功");
  15. } catch (Exception e) {
  16. System.out.println("失敗");
  17. e.printStackTrace();
  18. }
  19. }
  20.  
  21. public void doZipEnc() {
  22. try {
  23. // 將壓縮流寫到內存
  24. // ZipOutputStream saos = new ZipOutputStream(new ByteArrayOutputStream(), this.zipModel);
  25. // 本地測試
  26. FileOutputStream f = new FileOutputStream("C:/Users/Louis/Desktop/test1.zip" ,true);
  27. ZipOutputStream saos = new ZipOutputStream(f, this.zipModel);
  28.  
  29. for(int i=0;i<2;i++){
  30. ZipParameters parameters = createZipParameters("test" + i + ".txt", "" + i + i + i);
  31. addNewStreamToZip(saos, ("test" + i + ".txt").getBytes(), parameters);
  32. }
  33. // saos.writeTo(f);
  34.  
  35. saos.finish();
  36. saos.close();
  37. f.close();
  38. //文件大小
  39. // System.out.println("size:" +(saos.toByteArray().length/1024));
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. */
  45. public ZipOutputStream createZipOutputStream(OutputStream outputStream){
  46. return new ZipOutputStream(outputStream, zipModel);
  47. }
  48.  
  49. public void addNewStreamToZip(ZipOutputStream outputStream, InputStream data, ZipParameters parameters) throws ZipException {
  50. ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
  51.  
  52. try{
  53. byte[] buffer = new byte[1024];
  54.  
  55. for(int i ; (i = data.read(buffer)) != -1 ; ){
  56. tempOut.write(buffer, 0, i);
  57. tempOut.flush();
  58. }
  59. }catch (Exception e) {
  60. throw new ZipException(e);
  61. }finally{
  62. try{
  63. data.close();
  64. tempOut.close();
  65. }catch (Exception e) {
  66. }
  67. }
  68.  
  69. addNewStreamToZip(outputStream, tempOut.toByteArray(), parameters);
  70. }
  71.  
  72. public void addNewStreamToZip(ZipOutputStream saos, byte[] data, ZipParameters parameters) throws ZipException {
  73. try {
  74. if (zipModel.getEndCentralDirRecord() == null) {
  75. throw new ZipException("invalid end of central directory record");
  76. }
  77. /*
  78. checkParameters(parameters);
  79.  
  80. saos.putNextEntry(null, parameters);
  81.  
  82. if (! parameters.getFileNameInZip().endsWith("/") &&
  83. ! parameters.getFileNameInZip().endsWith("\\")
  84. ) {
  85. saos.write(data);
  86. }
  87. */
  88. saos.putNextEntry(null, parameters);
  89.  
  90. saos.write(data);
  91.  
  92. saos.closeEntry();
  93. } catch (ZipException e) {
  94. throw e;
  95. } catch (Exception e) {
  96. throw new ZipException(e);
  97. }
  98. }
  99.  
  100. public ZipParameters createZipParameters(String fileNameInZip, String password){
  101. ZipParameters parameters = new ZipParameters();
  102.  
  103. parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 壓縮方式
  104. parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 壓縮等級
  105. parameters.setSourceExternalStream(true);
  106. parameters.setFileNameInZip(fileNameInZip);
  107.  
  108. // 若有多檔,password個別有效
  109. if(password != null && ! password.trim().isEmpty()) {
  110. parameters.setEncryptFiles(true);
  111. parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
  112. parameters.setPassword(password);
  113. }
  114.  
  115. return parameters;
  116. }
  117. /*
  118. private void checkParameters(ZipParameters parameters) throws ZipException {
  119.  
  120. if (parameters == null) {
  121. throw new ZipException("cannot validate zip parameters");
  122. }
  123.  
  124. if ((parameters.getCompressionMethod() != Zip4jConstants.COMP_STORE) &&
  125. parameters.getCompressionMethod() != Zip4jConstants.COMP_DEFLATE) {
  126. throw new ZipException("unsupported compression type");
  127. }
  128.  
  129. if (parameters.getCompressionMethod() == Zip4jConstants.COMP_DEFLATE) {
  130. if (parameters.getCompressionLevel() < 0 && parameters.getCompressionLevel() > 9) {
  131. throw new ZipException("invalid compression level. compression level dor deflate should be in the range of 0-9");
  132. }
  133. }
  134.  
  135. if (parameters.isEncryptFiles()) {
  136. if (parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_STANDARD &&
  137. parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_AES) {
  138. throw new ZipException("unsupported encryption method");
  139. }
  140.  
  141. if (parameters.getPassword() == null || parameters.getPassword().length <= 0) {
  142. throw new ZipException("input password is empty or null");
  143. }
  144. } else {
  145. parameters.setAesKeyStrength(-1);
  146. parameters.setEncryptionMethod(-1);
  147. }
  148. }
  149. */
  150. }


用法:
  1. ZipUtil zipUtil = new ZipUtil();
  2.  
  3. ZipOutputStream zipOutputStream = zipUtil.createZipOutputStream(outputStream);
  4.  
  5. ZipParameters parameters = zipUtil.createZipParameters(fileNameInZip, zipPwd);
  6.  
  7. zipUtil.addNewStreamToZip(zipOutputStream, input, parameters);
  8.  
  9. zipOutputStream.finish();
  10. zipOutputStream.close();


第3行這邊可以自由定義需要的OutputStream
第5行
fileNameInZip:在zip檔中檔案的名字
(zip檔中允許相同名字的檔案,有問題的只會發生在解zip的時候一直會問你要不要覆蓋相同的檔案)
zipPwd:密碼是個別有效的

沒有留言:

張貼留言