Java IO流文件压缩与解压

文件的压缩

* ZipEntry

*ZipOutputStream

 

文件的解压

*ZipInputStream

* ZipEntry

*ZipFile

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipFileTest {
   public static void main(String[] args) {
      zipDirectorys(new File("E:\\java学习资料\\学习笔记\\Java笔记"));
   }

   /**
    * 压缩单个文件
    */
   public static void zipSingleFile(File file) {
      try {
        FileInputStream fis = new FileInputStream(file);
        String zipPath = file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("."))+ ".zip";
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
        zos.putNextEntry(new ZipEntry(file.getName()));
        int len = 0;
        byte[] buff = new byte[10240];
        while ((len = fis.read(buff)) != -1) {
           zos.write(buff, 0, len);
           zos.flush();
        }
        zos.finish();
        zos.close();
        fis.close();
        System.out.println("压缩完成");
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
   }

   /**
    * 压缩简单文件夹
    */
   public static void zipSingleDirectory(File file) {
      try {
        String zipPath = file.getAbsolutePath() + ".zip";
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
        File[] files = file.listFiles();
        for (File f : files) {
           FileInputStream fis = new FileInputStream(f);
           zos.putNextEntry(new ZipEntry(f.getName() + "\\" + f.getName()));
           int len = 0;
           byte[] buff = new byte[10240];
           while ((len = fis.read(buff)) != -1) {
              zos.write(buff, 0, len);
              zos.flush();
           }
           fis.close();
        }
        zos.finish();
        zos.close();
        System.out.println("压缩完成");
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
   }

   /**
    * 压缩复杂文件夹
    */
   public static void zipDirectorys(File file) {
      try {
        String zipPath = file.getAbsolutePath() + ".zip";
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
        int index = file.getAbsolutePath().lastIndexOf("\\");
        zip(file, zos, index);
        zos.finish();
        zos.close();
        System.out.println("压缩完成");
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
   }

   private static void zip(File file, ZipOutputStream zos, int index) {
      if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (File f : files) {
           zip(f, zos, index);
        }
      } else {
        try {
           zos.putNextEntry(new ZipEntry(file.getAbsolutePath().substring(index + 1)));
           FileInputStream fis = new FileInputStream(file);
           int len = 0;
           byte[] buff = new byte[10240];
           while ((len = fis.read(buff)) != -1) {
              zos.write(buff, 0, len);
              zos.flush();
           }
           fis.close();
        } catch (IOException e) {
           e.printStackTrace();
        }
      }
   }

   /**
    * 复杂zip文件的解压
    */

   public static void zipInput(File file) {
      try {
        String filePath = file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("."));
        System.out.println(filePath);
        ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
        ZipEntry zipEntry = null;
        while ((zipEntry = zis.getNextEntry()) != null) {
           if (!zipEntry.isDirectory()) {
              File f = new File(filePath + "\\" + zipEntry.getName());
              f.getParentFile().mkdirs();
              System.out.println(f.getAbsolutePath());
              FileOutputStream fos = new FileOutputStream(f);
              int len = 0;
              byte[] buff = new byte[10240];
              while ((len = zis.read(buff, 0, len)) != 0) {
                 fos.write(buff, 0, len);
                 fos.flush();
              }
              fos.close();
           }
        }
        zis.closeEntry();
        zis.close();
        System.out.println("解压完成");
      } catch (Exception e) {
        e.printStackTrace();
      }
   }
}