操作 ZIP - 图1

解压 zip 文件

  1. import java.io.*;
  2. import java.util.zip.ZipEntry;
  3. import java.util.zip.ZipInputStream;
  4. public class ZipOperation {
  5. public static void main(String[] args) {
  6. ZipTool z = new ZipTool();
  7. try{
  8. z.unzipFile("./java.zip","./test/");
  9. }catch (IOException e){
  10. e.printStackTrace();
  11. }
  12. }
  13. }
  14. class ZipTool{
  15. public int unzipFile(String filePath,String desc) throws IOException {
  16. ZipInputStream zip = null;
  17. ZipEntry entry = null;
  18. OutputStream out = null;
  19. InputStream sourceFilePath = new FileInputStream(filePath);
  20. int flag = -1;
  21. try{
  22. zip = new ZipInputStream(sourceFilePath);
  23. while ((entry = zip.getNextEntry())!=null){
  24. String path = entry.getName();
  25. if (entry.isDirectory()){
  26. System.out.println("文件夹路径为: " + path);
  27. File descFilePath = new File(desc+path);
  28. if (!descFilePath.exists()){
  29. descFilePath.mkdirs();
  30. }
  31. }else{
  32. // 这里应该是 文件夹 + 文件名 才是写入的路径
  33. out = new FileOutputStream(desc+path);
  34. byte[] buff = new byte[1024];
  35. int len;
  36. // zip.read(buff)
  37. while((len = zip.read(buff)) > 0 ){
  38. out.write(buff,0,len);
  39. }
  40. }
  41. }
  42. flag = 1;
  43. }finally {
  44. zip.close();
  45. out.close();
  46. }
  47. return flag;
  48. }
  49. }

压缩文件

  1. /*
  2. * @Description: For zip file and unzip file and read zipfile.
  3. * @author: johnw;
  4. * @Created: 6/23/2020;
  5. */
  6. import java.io.*;
  7. import java.util.zip.ZipEntry;
  8. import java.util.zip.ZipInputStream;
  9. import java.util.zip.ZipOutputStream;
  10. public class ZipOperation {
  11. public static void main(String[] args) {
  12. ZipTool z = new ZipTool();
  13. try{
  14. z.zipFile("src", "./test.zip");
  15. }catch (IOException e){
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  20. class ZipTool{
  21. public int zipFile(String filePath, String zipFilePath) throws IOException{
  22. // 新建一个 ZIP 输出的管道
  23. ZipOutputStream zip = null;
  24. // 新建一个文件输出的管道
  25. OutputStream out = new FileOutputStream(zipFilePath);
  26. // 新建一个读取文件的管道,用于将文件写入 zip 包里面
  27. InputStream readZipFile = null;
  28. // 要读取文件,就要使用到文件路径
  29. String realFilePath;
  30. try{
  31. zip = new ZipOutputStream(out);
  32. File files = new File(filePath);
  33. // 列出所有的文件夹以及文件
  34. for(File file : files.listFiles()){
  35. realFilePath = filePath +"/"+ file.getName();
  36. zip.putNextEntry(new ZipEntry(realFilePath));
  37. // 开始读取
  38. readZipFile = new FileInputStream(realFilePath);
  39. int b;
  40. // 写入zip的操作
  41. while((b = readZipFile.read())!= -1){
  42. zip.write(b);
  43. }
  44. }
  45. }finally {
  46. zip.close();
  47. out.close();
  48. readZipFile.close();
  49. }
  50. return -1;
  51. }
  52. }

读取压缩文件

有些时候我们只需要读取,并不需要解压操作

/*
 * @Description: For zip file and unzip file and  read zipfile.
 * @author: johnw;
 * @Created: 6/23/2020;
 */

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipOperation {
    public static void main(String[] args) {
        ZipTool z = new ZipTool();
        try{
            //z.unzipFile("./java.zip","./test/");
            //z.zipFile("src", "./test.zip");
            z.readFile("./test.zip");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

class ZipTool{

    public void readFile(String filePath)throws IOException{
        // 先准备一个 zip 管道
        ZipInputStream zip = null;
        // 再准备一个文件读取管道
        InputStream in = null;

        ZipEntry entry = null;
        try{
            // Zip 管道套上 文件读取管道, 就可以读取zip 文件信息了
            in = new FileInputStream(filePath);
            zip = new ZipInputStream(in);
            // 循环遍历读取路径
            while((entry = zip.getNextEntry()) != null) {
                String fileName = entry.getName();
                System.out.println(fileName);
            }

        }finally {
            zip.close();
            in.close();
        }
    }
}

Code: https://github.com/01x01/Java/blob/master/src/ZipOperation.java