开发者社区> 问答> 正文

关于文件的压缩ZipOutputStream:报错

/*
 *  压缩
 */
public class Compression {
 
 //要压缩文件的路径
 private String srcPath;
 
 //压缩后存放的路径
 private String dirPath;
 
 
 public Compression(String srcPath, String dirPath) {
  // TODO Auto-generated constructor stub
  this.srcPath = srcPath;
  this.dirPath = dirPath;
 }
 
 /*
  *  压缩程序
  */
 public void compressionProgram() {
  ZipOutputStream zipOutputStream = null;
  
  File file = null;
  
  try {
   file = new File(srcPath);
   
   zipOutputStream = new ZipOutputStream(new FileOutputStream(dirPath));
   
   ZipEntry zipEntryFile = new ZipEntry(file.getName());
   
   zipOutputStream.putNextEntry(zipEntryFile);
   
   if(file.isDirectory()) {//文件夹
    compressionFolder(file, zipOutputStream);
   } else {//文件
    compressionFile(file, zipOutputStream);
   }
   
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   try {
    if(zipOutputStream != null) {
     zipOutputStream.close();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 
 /*
  *  压缩文件
  *  参数: 当前访问的文件file,  压缩流zipCompression
  */
 private void compressionFile(File file, ZipOutputStream zipOutputStream){
  
  FileInputStream fileInputStream = null;
  
  try {
   fileInputStream = new FileInputStream(file);
   
   byte[] byteStr = new byte[1024 * 1024]; // 每次读取1M
   
   int strLength = fileInputStream.read(byteStr);
   
   while(strLength != -1) {
    zipOutputStream.write(byteStr, 0, strLength);
    
    strLength = fileInputStream.read(byteStr);
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   new Throwable("文件不存在!");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   try {
    if(fileInputStream != null) {
     fileInputStream.close();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

 /*
  *  压缩文件夹
  */
 private void compressionFolder(File file, ZipOutputStream zipOutputStream) throws FileNotFoundException {
  
  FileInputStream fileInputStream = null;
  
  File[] files = null;
  
  try {
   
   files = file.listFiles(); //得到该文件夹下的所有文件
   
   for (File filesFile : files) {
    if(filesFile.isDirectory()) {
     zipOutputStream.putNextEntry(new ZipEntry(filesFile.getName()));
     compressionFolder(file, zipOutputStream);
    } else {
     compressionFile(file, zipOutputStream);
    }
   }
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}



压缩文件夹不成功。在压缩文件夹中这句zipOutputStream.putNextEntry(new ZipEntry(filesFile.getName()));
有问题

展开
收起
kun坤 2020-06-07 22:29:28 828 0
1 条回答
写回答
取消 提交回答
  •         if (filesFile.isDirectory()) {  
                zipOutputStream.putNextEntry(new ZipEntry(filesFile.getName() + "/"));  
                File[] files = filesFile.listFiles();  
                for (File file : files) {  
                    compressionFolder(file, zipOutputStream);  
                }  
            } 
    ######我试了一下,不行。我想在这个函数中做递归。######public class Compression {


      // 要压缩文件的路径
      private String srcPath;


      // 压缩后存放的路径
      private String dirPath;


      public Compression(String srcPath, String dirPath) {
        this.srcPath = srcPath;
        this.dirPath = dirPath;
      }


      /**
       * 压缩程序
       /
      public void compressionProgram() {
        ZipOutputStream zipOutputStream = null;
        File file = null;


        try {
          file = new File(srcPath);
          zipOutputStream = new ZipOutputStream(new FileOutputStream(dirPath));


          if(file.isDirectory()) {//文件夹
            compressionFolder(file, zipOutputStream, "");
          } else {//文件
            zipOutputStream.putNextEntry(new ZipEntry(""));
            compressionFile(file, zipOutputStream, "");
          }


        } catch (FileNotFoundException  e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            if(zipOutputStream != null) {
             zipOutputStream.close();
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        } // try
     }


      /**
       
     压缩文件
       *  参数: 当前访问的文件file,  压缩流zipCompression
       /
      private void compressionFile(File file, ZipOutputStream zipOutputStream, String dir) {
        FileInputStream fileInputStream = null;
        try {


          zipOutputStream.putNextEntry(new ZipEntry(dir + file.getName()));


          fileInputStream = new FileInputStream(file);
          byte[] byteStr = new byte[1024 * 1024]; // 每次读取1M
          int strLength = -1;


          while((strLength = fileInputStream.read(byteStr) ) > 0) {
            zipOutputStream.write(byteStr, 0, strLength);
          }


        } catch (FileNotFoundException e) {
          new Throwable("文件不存在!");
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            if(fileInputStream != null) {
              fileInputStream.close();
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        } // try
     }


      /**
       
     压缩文件夹
       */
      private void compressionFolder(File file, ZipOutputStream zipOutputStream, String dir) throws FileNotFoundException {
        String path = dir + file.getName() + "/";
        try {
          zipOutputStream.putNextEntry(new ZipEntry(path));


          File[] files = file.listFiles(); //得到该文件夹下的所有文件
          for (File filesFile : files) {


            if(filesFile.isDirectory()) {
              compressionFolder(filesFile, zipOutputStream, path);
            } else {
              compressionFile(filesFile, zipOutputStream, path);
            }


          } // for


        } catch (IOException e) {
           e.printStackTrace();
        }
      }
    }


    ######

    引用来自“离未罔两”的评论

    public class Compression {


      // 要压缩文件的路径
      private String srcPath;


      // 压缩后存放的路径
      private String dirPath;


      public Compression(String srcPath, String dirPath) {
        this.srcPath = srcPath;
        this.dirPath = dirPath;
      }


      /**
       * 压缩程序
       */
      public void compressionProgram() {
        ZipOutputStream zipOutputStream = null;
        File file = null;


        try {
          file = new File(srcPath);
          zipOutputStream = new ZipOutputStream(new FileOutputStream(dirPath));


          if(file.isDirectory()) {//文件夹
            compressionFolder(file, zipOutputStream, "");
          } else {//文件
            zipOutputStream.putNextEntry(new ZipEntry(""));
            compressionFile(file, zipOutputStream, "");
          }


        } catch (FileNotFoundException  e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            if(zipOutputStream != null) {
             zipOutputStream.close();
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        } // try
     }


      /**
       *  压缩文件
       *  参数: 当前访问的文件file,  压缩流zipCompression
       */
      private void compressionFile(File file, ZipOutputStream zipOutputStream, String dir) {
        FileInputStream fileInputStream = null;
        try {


          zipOutputStream.putNextEntry(new ZipEntry(dir + file.getName()));


          fileInputStream = new FileInputStream(file);
          byte[] byteStr = new byte[1024 * 1024]; // 每次读取1M
          int strLength = -1;


          while((strLength = fileInputStream.read(byteStr) ) > 0) {
            zipOutputStream.write(byteStr, 0, strLength);
          }


        } catch (FileNotFoundException e) {
          new Throwable("文件不存在!");
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            if(fileInputStream != null) {
              fileInputStream.close();
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        } // try
     }


      /**
       *  压缩文件夹
       */
      private void compressionFolder(File file, ZipOutputStream zipOutputStream, String dir) throws FileNotFoundException {
        String path = dir + file.getName() + "/";
        try {
          zipOutputStream.putNextEntry(new ZipEntry(path));


          File[] files = file.listFiles(); //得到该文件夹下的所有文件
          for (File filesFile : files) {


            if(filesFile.isDirectory()) {
              compressionFolder(filesFile, zipOutputStream, path);
            } else {
              compressionFile(filesFile, zipOutputStream, path);
            }


          } // for


        } catch (IOException e) {
           e.printStackTrace();
        }
      }
    }

       晚上重新改了一下,加入路径,但不是很理解ZipEntry这个类。只有在绝对路径下可以压缩所有文件。准备早上再思考一下。看见了这篇答案,试了一下,可以压缩而且不是在绝对路径下,很好的解决了这个问题。谢谢了!!!我在研究研究代码。
    2020-06-07 22:29:37
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载