Java 解压,压缩 zip文件

分类:

/

时间:

项目中用到对文件解压,压缩。于是自己就编写了一段

  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.ArrayList;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import java.util.zip.ZipEntry;
  11. import java.util.zip.ZipFile;
  12. import java.util.zip.ZipInputStream;
  13. import java.util.zip.ZipOutputStream;
  14. public class ZipUtil {
  15. public static void unZip(String src,String target) throws IOException{
  16. ZipInputStream zipIn=null;
  17. ZipFile zip=null;
  18. try {
  19. zipIn = new ZipInputStream(new FileInputStream(src));
  20. ZipEntry in=null;
  21. zip=new ZipFile(src);
  22. while((in=zipIn.getNextEntry())!=null){
  23. File file=new File(target+in.getName());
  24. if(in.getName().endsWith("/")){
  25. file.mkdirs();
  26. }
  27. else{
  28. byte[] b=IOUtil.getByteByInputStream(zip.getInputStream(in));
  29. if(!new File(file.getParent()).exists()){
  30. new File(file.getParent()).mkdirs();
  31. }
  32. FileOutputStream fout=new FileOutputStream(file);
  33. fout.write(b);
  34. fout.close();
  35. }
  36. }
  37. zip.close();
  38. } finally{
  39. if(zipIn!=null){
  40. zipIn.close();
  41. }
  42. if(zip!=null){
  43. zip.close();
  44. }
  45. }
  46. }
  47. public static void inZip(List<File> files,String basePath,String target) throws IOException{
  48. List<File> cfiles=new ArrayList<File>();
  49. for (File file : files){
  50. cfiles.add(file);
  51. }
  52. for (File file : cfiles) {
  53. if(file.isDirectory()){
  54. IOUtil.getAllFiles(file.toString(), files);
  55. }
  56. }
  57. ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(target));
  58. for (File file : files) {
  59. System.out.println(file);
  60. ZipEntry entry = null;
  61. byte[] b = new byte[1024];
  62. int len = 0;
  63. if (file.isFile()) {
  64. entry = new ZipEntry(file.toString().substring(basePath.length()));
  65. zos.putNextEntry(entry);
  66. InputStream is = new BufferedInputStream(new FileInputStream(file));
  67. while ((len = is.read(b, 0, b.length)) != -1) {
  68. zos.write(b, 0, len);
  69. }
  70. is.close();
  71. }
  72. else{
  73. }
  74. }
  75. zos.close();
  76. }
  77. public static void main(String[] args) {
  78. try {
  79. List<File> files=new LinkedList<File>();
  80. files.add(new File("D:/tmp"));
  81. files.add(new File("D:/t2.jpg"));
  82. inZip(files,"D:/","D:/1.zip");
  83. unZip("D:/1.zip", "D:/tmp/");
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }

依赖的IOUtil

  1. import java.io.ByteArrayOutputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.UnsupportedEncodingException;
  9. import java.util.List;
  10. public class IOUtil {
  11. public static final byte[] getByteByInputStream(InputStream in) {
  12. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  13. byte[] tempByte = new byte[1024];
  14. try {
  15. int length = 0;
  16. while ((length = in.read(tempByte)) != -1) {
  17. bout.write(tempByte, 0, length);
  18. }
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } finally {
  22. try {
  23. in.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. return bout.toByteArray();
  29. }
  30. public static final String getStringInputStream(InputStream in) {
  31. try {
  32. return new String(getByteByInputStream(in),"UTF-8");
  33. } catch (UnsupportedEncodingException e) {
  34. e.printStackTrace();
  35. }
  36. return null;
  37. }
  38. public static final void getAllFiles(String path, List<File> files) {
  39. getAllFilesByProfix(path, null, files);
  40. }
  41. public static final void getAllFilesByProfix(String path, String suffix,
  42. List<File> files) {
  43. File file = new File(path);
  44. if (file.isDirectory()) {
  45. File[] fs = file.listFiles();
  46. if (fs != null) {
  47. for (File file2 : fs) {
  48. if (file2.isDirectory()) {
  49. getAllFilesByProfix(file2.getPath(), suffix, files);
  50. } else {
  51. if (suffix != null) {
  52. if (file2.toString().endsWith(suffix)) {
  53. files.add(file2);
  54. }
  55. } else {
  56. files.add(file2);
  57. }
  58. }
  59. }
  60. }
  61. } else {
  62. if (suffix != null) {
  63. if (file.toString().endsWith(suffix)) {
  64. files.add(file);
  65. }
  66. } else {
  67. files.add(file);
  68. }
  69. }
  70. }
  71. public static void moveOrCopy(String filer,String tag,boolean isMove)
  72. {
  73. System.out.println(filer);
  74. File f=new File(filer);
  75. if(f.isDirectory())
  76. {
  77. File fs[]=new File(filer).listFiles();
  78. tag=tag+"/"+f.getName();
  79. new File(tag).mkdirs();
  80. for(File fl:fs)
  81. {
  82. if(fl.isDirectory())
  83. {
  84. moveOrCopy(fl.toString(),tag,isMove);
  85. }
  86. else
  87. {
  88. moveOrCopyFile(fl.toString(),tag+"/"+fl.getName(),isMove);
  89. }
  90. }
  91. }
  92. else{
  93. moveOrCopyFile(f.toString(),tag+"/"+f.getName(),isMove);
  94. }
  95. }
  96. public static void writeStrToFile(String str,File file){
  97. writeBytesToFile(str.getBytes(), file);
  98. }
  99. public static void writeBytesToFile(byte[] bytes,File file){
  100. try {
  101. FileOutputStream out = new FileOutputStream(file);
  102. out.write(bytes);
  103. out.close();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. public static void moveOrCopyFile(String src, String tag, boolean isMove) {
  109. try {
  110. long s = System.currentTimeMillis();
  111. File f = new File(src);
  112. FileInputStream in = new FileInputStream(f);
  113. new File(tag).getParentFile().mkdirs();
  114. FileOutputStream out = new FileOutputStream(tag);
  115. // 小于1M(大小根据自己的情况而定)的文件直接一次性写入。
  116. byte[] b = new byte[1024];
  117. int length = 0;
  118. while ((length = in.read(b)) != -1) {
  119. out.write(b, 0, length);
  120. }
  121. // 一定要记得关闭流额。 不然其他程序那个文件无法进行操作
  122. in.close();
  123. out.close();
  124. System.out.println(System.currentTimeMillis() - s);
  125. if (isMove) {
  126. f.delete();
  127. }
  128. } catch (FileNotFoundException e) {
  129. e.printStackTrace();
  130. } catch (IOException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134. }

理解了ZipEntry 对编写代码有一定帮助(与Map.Entry类似)。 其实就是说其中的文件的意思。

最后更新:2015-06-28 03:45:47.0

—— 原创内容,转载请注明:[ 文章转载自:小春 ' s blog   //xiaochun.zrlog.com/ ] ——