用java解压文件

用java解压文件,java,zip,unzip,Java,Zip,Unzip,这是我用来将文件夹解压缩到特定位置的代码。一旦我执行了这个过程,我就会遇到以下异常。请告知为什么会出现此问题 public void unZipFile(String zipFileLocation, String outputFolder) { logger.info("ZipFileLocation: "+zipFileLocation); logger.info("OutputLocation: "+outputFolder); File dir = new Fi

这是我用来将文件夹解压缩到特定位置的代码。一旦我执行了这个过程,我就会遇到以下异常。请告知为什么会出现此问题

public void unZipFile(String zipFileLocation, String outputFolder) {
    logger.info("ZipFileLocation: "+zipFileLocation);
    logger.info("OutputLocation: "+outputFolder);

    File dir = new File(outputFolder);
    // create output directory if it doesn't exist
    if(!dir.exists()) dir.mkdirs();
    FileInputStream fis;
    //buffer for read and write data to file
    byte[] buffer = new byte[1024];
    try {
        fis = new FileInputStream(zipFileLocation);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry ze = zis.getNextEntry();
        while(ze != null){
            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            System.out.println("Unzipping to "+newFile.getAbsolutePath());
            //create directories for sub directories in zip
            new File(newFile.getParent()).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            //close this ZipEntry
            zis.closeEntry();
            ze = zis.getNextEntry();
        }
        //close last ZipEntry
        zis.closeEntry();
        zis.close();
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
如果有人能帮我解决这个问题,我将不胜感激


谢谢,

我不知道为什么在您的情况下失败(zip文件无效?),但我建议:

  • 首先,使用Java7java.nio.file.Path API,它比旧的Java.io.file要好得多
  • 第二,它在处理压缩文件时使用起来更方便。它更灵活,可以处理zip、bzip、7zip、pack200等格式
  • 最后,在try中执行.close()操作。。。最后阻塞以避免异常情况下未关闭的流

    • 我可以想到以下原因:

    • 该文件可能已损坏。请尝试使用zip实用程序打开该文件
    • 文本文件的编码可能有问题
    • 文件需要以“二进制”模式读取/传输
    • 行结尾可能有问题\n或\r\n

    • 使用此库www.lingala.net/zip4j/

      将此jar文件添加到应用程序的lib文件夹中

      这样检查您的导入

      导入net.lingala.zip4j.core.ZipFile

      导入net.lingala.zip4j.exception.zipeexception

      导入net.lingala.zip4j.model.FileHeader

      使用下面这样的方法

      解压(“/sdcard/file.zip”,“/sdcard/unzipFolder”)


      而不是将其硬编码为

      
        public static void unzip(String Filepath, String DestinationFolderPath) {
      
              try {
                  ZipFile zipFile = new ZipFile(Filepath);
                  List fileHeaders = zipFile.getFileHeaders();
                  for(int i=0;i<fileHeaders.size();i++) {
                      FileHeader  fileHeader=(FileHeader) fileHeaders.get(i);
                      String fileName = fileHeader.getFileName();
                      Log.d(TAG,fileName);
                      if (fileName.contains("\\")) {
                          fileName=fileName.replace("\\","\\\\");
                          String[] Folders=fileName.split("\\\\");
                          StringBuilder newFilepath=new StringBuilder();
                          newFilepath.append(DestinationFolderPath);
                          for (int j=0;j<Folders.length-1;j++){
                              newFilepath.append(File.separator);
                              newFilepath.append(Folders[j]);
                          }
                          zipFile.extractFile(fileHeader, newFilepath.toString(),null,Folders[Folders.length-1]);
                      }else {
                          zipFile.extractFile(fileHeader,DestinationFolderPath);
                      }
                  }
              } catch (ZipException e) {
                  e.printStackTrace();
              }
          }
      
      
      试试下面

      byte[] buffer = new byte[1024];
      
      你可以在这里找到详细的教程


      我遇到了类似的问题,我修改了代码以使其正常工作,结果发现代码没有区分文件和文件夹,这就是我添加的检查

      byte[] buffer = new byte[(int) ze.getSize()];
      

      刚才提到了下面的异常,请查看编辑版本,我希望您的ZIP文件已损坏。你能从命令行解压它吗?你是不是在没有设置二进制模式的情况下通过FTP发送的?如何设置二进制模式?你能尝试使用另一个zip文件吗?这会有帮助吗?
      byte[] buffer = new byte[(int) ze.getSize()];
      
          public static void unzipFile(String zipFilePath, String destDir)
          {
              File dir = new File(destDir);
              // create output directory if it doesn't exist
              if (!dir.exists())
                  dir.mkdirs();
              FileInputStream fis;
              // buffer for read and write data to file
              byte[] buffer = new byte[1024];
              try
              {
                  fis = new FileInputStream(zipFilePath);
                  ZipInputStream zis = new ZipInputStream(fis);
                  ZipEntry ze = zis.getNextEntry();
                  while (ze != null)
                  {
                      try
                      {
                          String fileName = ze.getName();
                          File newFile = new File(destDir + File.separator + fileName);
                          System.out.println("Unzipping to " + newFile.getAbsolutePath());
                          // create directories for sub directories in zip
                          new File(newFile.getParent()).mkdirs();
                          if(ze.isDirectory()) // check if this is a diectory or file
                          {
                              newFile.mkdirs();
                          }
                          else
                          {
                              FileOutputStream fos = new FileOutputStream(newFile);
                              int len;
                              while ((len = zis.read(buffer)) > 0)
                              {
                                  fos.write(buffer, 0, len);
                              }
                              fos.close();
                          }
                          // close this ZipEntry
                          zis.closeEntry();
                      }
                      catch(Exception e)
                      {
                          System.err.println(e.getMessage());
                      }
                      ze = zis.getNextEntry();
                  }
                  // close last ZipEntry
                  zis.closeEntry();
                  zis.close();
                  fis.close();
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
          }