Java实现Zip压缩与解压(解决中文乱码问题)

  • Post author:
  • Post category:java




解决中文压缩与解压问题

使用的是org.apache.tools.zip包下面的相关类
下面是自己写的类,可以设置和获取Zip文件的注释信息

Java代码


  1. import


    java.io.File;



  2. import


    java.io.FileInputStream;



  3. import


    java.io.FileOutputStream;



  4. import


    java.io.InputStream;



  5. import


    java.util.ArrayList;



  6. import


    java.util.Enumeration;



  7. import


    java.util.List;




  8. import


    org.apache.tools.zip.ZipEntry;



  9. import


    org.apache.tools.zip.ZipFile;



  10. import


    org.apache.tools.zip.ZipOutputStream;





  11. public




    class


    ZipUtil {




  12. private


    String comment =


    “”


    ;




  13. public




    void


    setComment(String comment) {



  14. this


    .comment = comment;


  15. }




  16. public




    void


    zip(String src, String dest, List filter)


    throws


    Exception {


  17. ZipOutputStream out =

    new


    ZipOutputStream(


    new


    FileOutputStream(dest));


  18. File srcFile =

    new


    File(src);


  19. zip(out,srcFile,

    “”


    ,filter);


  20. out.close();

  21. }




  22. public




    void


    zip(ZipOutputStream out, File srcFile, String base, List filter)


    throws


    Exception {



  23. if


    (srcFile.exists()==


    false


    ) {



  24. throw




    new


    Exception(


    “压缩目录不存在!”


    );


  25. }



  26. if


    (srcFile.isDirectory()) {


  27. File[] files = srcFile.listFiles();

  28. base = base.length() ==

    0


    ?


    “”


    : base +


    “/”


    ;



  29. if


    (isExist(base, filter)) {


  30. out.putNextEntry(

    new


    ZipEntry(base));


  31. }


  32. for


    (


    int


    i=


    0


    ; i<files.length; i++) {


  33. zip(out,files[i],base + files[i].getName(),filter);

  34. }

  35. }

    else


    {



  36. if


    (isExist(base, filter)) {


  37. base = base.length() ==

    0


    ? srcFile.getName() : base ;


  38. ZipEntry zipEntry =

    new


    ZipEntry(base);


  39. zipEntry.setComment(comment);

  40. out.putNextEntry(zipEntry);

  41. FileInputStream in =

    new


    FileInputStream(srcFile);



  42. int


    length =


    0


    ;



  43. byte


    [] b =


    new




    byte


    [


    1024


    ];



  44. while


    ((length=in.read(b,


    0


    ,


    1024


    ))!=-


    1


    ) {


  45. out.write(b,

    0


    ,length);


  46. }

  47. in.close();

  48. }

  49. }

  50. }




  51. public




    boolean


    isExist(String base, List list) {



  52. if


    (list !=


    null


    && !list.isEmpty()) {



  53. for


    (


    int


    i =


    0


    ; i < list.size(); i++) {



  54. if


    (base.indexOf((String) list.get(i)) >=


    0


    ) {



  55. return




    true


    ;


  56. }

  57. }

  58. }


  59. return




    false


    ;


  60. }




  61. public




    void


    unZip(String srcFile,String dest,


    boolean


    deleteFile)


    throws


    Exception {


  62. File file =

    new


    File(srcFile);



  63. if


    (!file.exists()) {



  64. throw




    new


    Exception(


    “解压文件不存在!”


    );


  65. }

  66. ZipFile zipFile =

    new


    ZipFile(file,”GB2312″);


  67. Enumeration e = zipFile.getEntries();


  68. while


    (e.hasMoreElements()) {


  69. ZipEntry zipEntry = (ZipEntry)e.nextElement();


  70. if


    (zipEntry.isDirectory()) {


  71. String name = zipEntry.getName();

  72. name = name.substring(

    0


    ,name.length()-


    1


    );


  73. File f =

    new


    File(dest + name);


  74. f.mkdirs();

  75. }

    else


    {


  76. File f =

    new


    File(dest + zipEntry.getName());


  77. f.getParentFile().mkdirs();

  78. f.createNewFile();

  79. InputStream is = zipFile.getInputStream(zipEntry);

  80. FileOutputStream fos =

    new


    FileOutputStream(f);



  81. int


    length =


    0


    ;



  82. byte


    [] b =


    new




    byte


    [


    1024


    ];



  83. while


    ((length=is.read(b,


    0


    ,


    1024


    ))!=-


    1


    ) {


  84. fos.write(b,

    0


    , length);


  85. }

  86. is.close();

  87. fos.close();

  88. }

  89. }



  90. if


    (zipFile !=


    null


    ) {


  91. zipFile.close();

  92. }



  93. if


    (deleteFile) {


  94. file.deleteOnExit();

  95. }

  96. }




  97. public




    static


    String getZipComment(String srcFile) {


  98. String comment =

    “”


    ;



  99. try


    {


  100. ZipFile zipFile =

    new


    ZipFile(srcFile);


  101. Enumeration e = zipFile.getEntries();



  102. while


    (e.hasMoreElements()) {


  103. ZipEntry ze = (ZipEntry) e.nextElement();


  104. comment = ze.getComment();


  105. if


    (comment !=


    null


    && !comment.equals(


    “”


    )


  106. && !comment.equals(

    “null”


    )) {



  107. break


    ;


  108. }

  109. }


  110. zipFile.close();

  111. }

    catch


    (Exception e) {


  112. System.out.println(

    “获取zip文件注释信息失败:”


    + e.getMessage());


  113. }



  114. return


    comment;


  115. }



  116. public




    static




    void


    main(String[] args)


    throws


    Exception {



  117. long


    begin = System.currentTimeMillis();


  118. ZipUtil zu =

    new


    ZipUtil();


  119. List<String> filter =

    new


    ArrayList<String>();


  120. filter.add(

    “3RDPARTY”


    );


  121. filter.add(

    “BANNER.GIF”


    );


  122. zu.setComment(

    “aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”


    );


  123. zu.zip(

    “C:/VALUEADD”


    ,


    “c:/hh.zip”


    ,filter);


  124. System.out.println(ZipUtil.getZipComment(

    “c:/hh.zip”


    ));



  125. //new ZipUtil().unZip(“c:/tt.zip”, “c:/mmmmmmmmmmmmmmmmmmm/”, true);





  126. //File f = new File(“c:/hh.zip”);





  127. //f.deleteOnExit();





  128. long


    end = System.currentTimeMillis();


  129. System.out.println(end-begin);

  130. }

  131. }






  132. 如果不需要上面的filter,可以添加fileter为空list或者用下面的程序:



  133. 不带filter的zip压缩:


  134. //





  135. // * @author WeiMiao





  136. // * @param out: ZipOutputStream





  137. // * @param srcFile: 要压缩的目录





  138. // * @param base: 根路径





  139. // * @throws Exception





  140. //





  141. public




    static




    void


    zip(ZipOutputStream out, File srcFile, String base)


    throws


    Exception {



  142. if


    (!srcFile.exists()) {



  143. throw




    new


    Exception(


    “压缩目录不存在!”


    );


  144. }


  145. if


    (srcFile.isDirectory()) {


  146. File[] files = srcFile.listFiles();

  147. base = base.length() ==

    0


    ?


    “”


    : base +


    “/”


    ;



  148. if


    (base.length() >


    0


    ) {


  149. out.putNextEntry(

    new


    ZipEntry(base));


  150. }


  151. for


    (


    int


    i =


    0


    ; i < files.length; i++) {


  152. zip(out, files[i], base + files[i].getName());

  153. }

  154. }

    else


    {


  155. base = base.length() ==

    0


    ? srcFile.getName() : base;


  156. out.putNextEntry(

    new


    ZipEntry(base));


  157. FileInputStream fis =

    new


    FileInputStream(srcFile);



  158. int


    length =


    0


    ;



  159. byte


    [] b =


    new




    byte


    [BUFFER];



  160. while


    ((length = fis.read(b,


    0


    , BUFFER)) != –


    1


    ) {


  161. out.write(b,

    0


    , length);


  162. }

  163. fis.close();

  164. }

  165. }