以压缩Zip文件为例。主要是通过ZipOutputStream类实现。解压缩主要使用ZipFile类和ZipInputStream以及ZipEntry类。

  1. package main;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.util.zip.*;
  6. public class Main
  7. {
  8.  
  9. public static final Integer BUFFERSIZE = 1024*1024;
  10. public static void main(String[] args) throws Exception
  11. {
  12. File fileNeedToBeCompressed = new File("C:" + File.separator + "D" + File.separator + "code" + File.separator + "output.xml");
  13. StringBuffer zipFilePath = new StringBuffer(fileNeedToBeCompressed.getParent());
  14. zipFilePath.append(File.separator).append(getFileName(fileNeedToBeCompressed.getName())).append(".zip");
  15. File compressedFile = new File(zipFilePath.toString());
  16.  
  17. InputStream is = null;
  18. ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(compressedFile));
  19.  
  20. if(fileNeedToBeCompressed.isDirectory())
  21. {
  22. //压缩一个文件夹
  23.  
  24. File[] files = fileNeedToBeCompressed.listFiles();
  25. for(File file : files)
  26. {
  27. compressFile(file, is, zos);
  28. }
  29. }
  30. else
  31. {
  32. //压缩一个文件
  33.  
  34. compressFile(fileNeedToBeCompressed,is,zos);
  35. }
  36. zos.close();
  37.  
  38. System.out.println("///~ Main done");
  39. }
  40.  
  41. public static String getFileName(String fileName)
  42. {
  43. if(fileName.lastIndexOf('.') < 0)
  44. {
  45. return fileName;
  46. }
  47. else
  48. {
  49. return fileName.substring(0,fileName.lastIndexOf('.'));
  50. }
  51. }
  52.  
  53. public static void compressFile(File fileNeedToBeCompressed,InputStream is,ZipOutputStream zos) throws Exception
  54. {
  55. System.out.println("正在压缩:" + fileNeedToBeCompressed.getName());
  56. is = new FileInputStream(fileNeedToBeCompressed);
  57. zos.putNextEntry(new ZipEntry(fileNeedToBeCompressed.getName()));
  58. int bytesReaded = 0;
  59. byte[] buffer = new byte[BUFFERSIZE];
  60. while((bytesReaded = is.read(buffer)) > 0)
  61. {
  62. zos.write(buffer,0,bytesReaded);
  63. }
  64. is.close();
  65. }
  66.  
  67. }

ZipInputStream 获取压缩文件中的每个ZipEntry,然后ZipFile通过ZipEntry拿到输入流。

  1. package main;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.util.zip.*;
  6. public class Main
  7. {
  8.  
  9. public static final Integer BUFFERSIZE = 1024*1024;
  10. public static void main(String[] args) throws Exception
  11. {
  12. // {
  13. // File fileNeedToBeCompressed = new File("C:" + File.separator + "D" + File.separator + "code" + File.separator + "resource");
  14. // StringBuffer zipFilePath = new StringBuffer(fileNeedToBeCompressed.getParent());
  15. // zipFilePath.append(File.separator).append(getFileName(fileNeedToBeCompressed.getName())).append(".zip");
  16. // File compressedFile = new File(zipFilePath.toString());
  17. //
  18. //
  19. // InputStream is = null;
  20. // ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(compressedFile));
  21. //
  22. // if(fileNeedToBeCompressed.isDirectory())
  23. // {
  24. // //压缩一个文件夹
  25. // File[] files = fileNeedToBeCompressed.listFiles();
  26. // for(File file : files)
  27. // {
  28. // compressFile(file, is, zos);
  29. // }
  30. // }
  31. // else
  32. // {
  33. // //压缩一个文件
  34. //
  35. // compressFile(fileNeedToBeCompressed,is,zos);
  36. // }
  37. // zos.close();
  38. // }
  39.  
  40. {
  41. String zipFilePath = "C:" + File.separator + "D" + File.separator + "code" + File.separator + "resource.zip";
  42. ZipFile zipFile = new ZipFile(zipFilePath);
  43. ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(zipFilePath)));
  44. ZipEntry tempZipEntry = null;
  45. OutputStream os = null;
  46. byte[] buffer = new byte[BUFFERSIZE];
  47. File unzipedFolder = new File(getFileName(zipFile.getName()));
  48. int bytesReaded = 0;
  49. if(!unzipedFolder.exists())
  50. unzipedFolder.mkdirs();
  51. while((tempZipEntry = zis.getNextEntry())!= null)
  52. {
  53. System.out.println("正在解压:" + tempZipEntry.getName());
  54. File newFile = new File(unzipedFolder.getPath() + File.separator + tempZipEntry.getName());
  55. InputStream is = zipFile.getInputStream(tempZipEntry);
  56. os = new FileOutputStream(newFile);
  57. while(( bytesReaded = is.read(buffer)) > 0)
  58. {
  59. os.write(buffer, 0, bytesReaded);
  60. }
  61. os.close();
  62. }
  63. zipFile.close();
  64. zis.close();
  65. }
  66. System.out.println("///~ Main done");
  67. }
  68.  
  69. public static String getFileName(String fileName)
  70. {
  71. if(fileName.lastIndexOf('.') < 0)
  72. {
  73. return fileName;
  74. }
  75. else
  76. {
  77. return fileName.substring(0,fileName.lastIndexOf('.'));
  78. }
  79. }
  80.  
  81. public static void compressFile(File fileNeedToBeCompressed,InputStream is,ZipOutputStream zos) throws Exception
  82. {
  83. System.out.println("正在压缩:" + fileNeedToBeCompressed.getName());
  84. is = new FileInputStream(fileNeedToBeCompressed);
  85. zos.putNextEntry(new ZipEntry(fileNeedToBeCompressed.getName()));
  86. int bytesReaded = 0;
  87. byte[] buffer = new byte[BUFFERSIZE];
  88. while((bytesReaded = is.read(buffer)) > 0)
  89. {
  90. zos.write(buffer,0,bytesReaded);
  91. }
  92. is.close();
  93. }
  94.  
  95. }

JAVA中压缩与解压缩的更多相关文章

  1. linux中压缩与解压缩命令小结

    linux中压缩与解压操作非常常见,其命令参数也非常的多,这里只介绍最经常用的带打包文件的几种压缩和解压方式和几个最常用的参数. 现在最常用的压缩和解压工具是gzip和bzip2,这两种工具不能相互解 ...

  2. Java实现压缩与解压缩

    import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...

  3. java 版本压缩、解压缩zip

    import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...

  4. java GZIP压缩与解压缩

    1.GZIP压缩 public static byte[] compress(String str, String encoding) { if (str == null || str.length( ...

  5. linux中压缩与解压缩命令

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...

  6. linux中压缩、解压缩命令详解

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  7. java ZipOutputStream压缩文件,ZipInputStream解压缩

    java中实现zip的压缩与解压缩.java自带的 能实现的功能比较有限. 本程序功能:实现简单的压缩和解压缩,压缩文件夹下的所有文件(文件过滤的话需要对File进一步细节处理). 对中文的支持需要使 ...

  8. JAVA中的deflate压缩实现

    在文件的传输过程中,为了使大文件能够更加方便快速的传输,一般采用压缩的办法来对文件压缩后再传输,JAVA中的java.util.zip包中的Deflater和Inflater类为使用者提供了DEFLA ...

  9. 利用JAVA API函数实现数据的压缩与解压缩

      综述 许多信息资料都或多或少的包含一些多余的数据.通常会导致在客户端与服务器之间,应用程序与计算机之间极大的数据传输量.最常见的解决数据存储和信息传送的方法是安装额外的存储设备和扩展现有的通讯能力 ...

随机推荐

  1. asp.net MVC ViewData详解

    转自:http://www.cnblogs.com/gaopin/archive/2012/11/13/2767515.html 控制器向视图中传值ViewData详解 1.将一个字符串传值到视图中 ...

  2. java 实现mysql数据库导出

    package com.zbb.util; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStre ...

  3. Oracle 11g R2 常用配置与日志的文件位置

    假设.bash_profile中oracle相关环境变量如下: $ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1 $ORACLE_BASE=/u01/a ...

  4. 剑指offer系列21--二叉搜索树的后续遍历序列

    * 21[题目]输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果. * 如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. * [注]二叉搜索树特点:左子树比根结 ...

  5. 装过photoshop后出现configuration error

    1.你用的应该是精简版的PS,找到ps启动图标,点击右键,以管理员身份运行试试. 2.可以右键你的快捷方式,选择兼容性,后面有个选框“以管理员身份运行”,应用,下次就不报错了.

  6. 关于学习Perl

    Perl是一门很有用的语言,可以用它来做很多事.然而,它也仅是一门语言,掌握了Perl,你只是掌握了Computer领域的一小块知识.在学习Perl前,请明确你的学习目的,并采用正确的学习方法和资源. ...

  7. 124. Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  8. C#中abstract和virtual区别

    在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在来讨论一下二者的区别.二者都牵涉到在派生类中与override的配合使用. 一.Virtual方法(虚方法) virtual ...

  9. 黄聪:wordpress后台加载ajax.googleapis.com导致打开速度很慢的解决方案

    打开wordpress后台,发现很卡,通过开发者工具看到是因为加载http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothn ...

  10. capture同focus

    SetCapture函数功能:该函数在属于当前线程的指定窗口里设置鼠标捕获.一旦窗口捕获了鼠标,所有鼠标输入都针对该窗口,无论光标是否在窗口的边界内.同一时刻只能有一个窗口捕获鼠标.如果鼠标光标在另一 ...