IO操作之使用zip包压缩和解压缩文件

时间:2023-03-08 21:22:09
IO操作之使用zip包压缩和解压缩文件

转自:http://www.cdtarena.com/java.html
​Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作。

我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作。

ZipFile

java中的每一个压缩文件都是可以使用ZipFile来进行表示的。
[java] view plaincopyprint?
  1. File file = new File("F:/zippath.zip");
  2. ZipFile zipFile = new ZipFile(file);
  3. System.out.println("压缩文件的名称为:" + zipFile.getName());
压缩单个文件
[java] view plaincopyprint?
  1. /** 压缩单个文件*/
  2. public static void ZipFile(String filepath ,String zippath) {
  3. try {
  4. File file = new File(filepath);
  5. File zipFile = new File(zippath);
  6. InputStream input = new FileInputStream(file);
  7. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
  8. zipOut.putNextEntry(new ZipEntry(file.getName()));
  9. int temp = 0;
  10. while((temp = input.read()) != -1){
  11. zipOut.write(temp);
  12. }
  13. input.close();
  14. zipOut.close();
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
应用:
[java] view plaincopyprint?
  1. ZipFile("d:/hello.txt", "d:/hello.zip");
压缩多个文件(文件夹)
[java] view plaincopyprint?
  1. /** 一次性压缩多个文件,文件存放至一个文件夹中*/
  2. public static void ZipMultiFile(String filepath ,String zippath) {
  3. try {
  4. File file = new File(filepath);// 要被压缩的文件夹
  5. File zipFile = new File(zippath);
  6. InputStream input = null;
  7. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
  8. if(file.isDirectory()){
  9. File[] files = file.listFiles();
  10. for(int i = 0; i < files.length; ++i){
  11. input = new FileInputStream(files[i]);
  12. zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
  13. int temp = 0;
  14. while((temp = input.read()) != -1){
  15. zipOut.write(temp);
  16. }
  17. input.close();
  18. }
  19. }
  20. zipOut.close();
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
应用:成都java培训
[java] view plaincopyprint?
  1. ZipMultiFile("f:/uu", "f:/zippath.zip");
解压缩单个文件 
[java] view plaincopyprint?
  1. /**  解压缩(解压缩单个文件)*/  www.cdtarena.com
  2. public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
  3. try {
  4. File file = new File(zippath);//压缩文件路径和文件名
  5. File outFile = new File(outfilepath);//解压后路径和文件名
  6. ZipFile zipFile = new ZipFile(file);
  7. ZipEntry entry = zipFile.getEntry(filename);//所解压的文件名
  8. InputStream input = zipFile.getInputStream(entry);
  9. OutputStream output = new FileOutputStream(outFile);
  10. int temp = 0;
  11. while((temp = input.read()) != -1){
  12. output.write(temp);
  13. }
  14. input.close();
  15. output.close();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
应用:
[java] view plaincopyprint?
  1. ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");
解压缩多个文件
ZipInputStream类:
当我们需要解压缩多个文件的时候,ZipEntry就无法使用了。
 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类。
[java] view plaincopyprint?
  1. /**  解压缩(压缩文件中包含多个文件)可代替上面的方法使用。
  2. * ZipInputStream类
  3. * 当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,
  4. * 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
  5. * */
  6. public static void ZipContraMultiFile(String zippath ,String outzippath){
  7. try {
  8. File file = new File(zippath);
  9. File outFile = null;
  10. ZipFile zipFile = new ZipFile(file);
  11. ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
  12. ZipEntry entry = null;
  13. InputStream input = null;
  14. OutputStream output = null;
  15. while((entry = zipInput.getNextEntry()) != null){
  16. System.out.println("解压缩" + entry.getName() + "文件");
  17. outFile = new File(outzippath + File.separator + entry.getName());
  18. if(!outFile.getParentFile().exists()){
  19. outFile.getParentFile().mkdir();
  20. }
  21. if(!outFile.exists()){
  22. outFile.createNewFile();
  23. }
  24. input = zipFile.getInputStream(entry);
  25. output = new FileOutputStream(outFile);
  26. int temp = 0;
  27. while((temp = input.read()) != -1){
  28. output.write(temp);
  29. }
  30. input.close();
  31. output.close();
  32. }
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
应用:
[java] view plaincopyprint?
  1. ZipContraMultiFile("f:/zippath.zip", "d:/");
  2. ZipContraMultiFile("d:/hello.zip", "d:/");