【问题标题】:Can't Unzip EPub File无法解压缩 Epub 文件
【发布时间】:2010-06-30 09:01:10
【问题描述】:

IMO,我认为 epub 是一种 zip 。因此,我尝试在a way 中解压缩。

public class Main {
    public static void main(String argv[ ])  {
        final int BUFFER = 2048;

    try {
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream("/Users/yelinaung/Documents/unzip/epub/doyle.epub");
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream("/Users/yelinaung/Documents/unzip/xml/");
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER))
                    != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    } catch ( IOException e) {
        e.printStackTrace();
    } 
  }
}

我得到了以下错误..

java.io.FileNotFoundException: /Users/yelinaung/Documents/unzip/xml (No such file or directory)

虽然我已经以这种方式创建了一个文件夹......并且

我解压 epub 的方式是否正确? ..纠正我

【问题讨论】:

    标签: java unzip epub


    【解决方案1】:

    我得到了答案.. 我没有检查必须提取的对象是否是文件夹。

    我以这种方式更正了。

    public static void main(String[] args) throws IOException {
            ZipFile zipFile = new ZipFile("/Users/yelinaung/Desktop/unzip/epub/doyle.epub");
            String path = "/Users/yelinaung/Desktop/unzip/xml/";
    
            Enumeration files = zipFile.entries();
    
            while (files.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) files.nextElement();
                if (entry.isDirectory()) {
                    File file = new File(path + entry.getName());
                    file.mkdir();
                    System.out.println("Create dir " + entry.getName());
                } else {
                    File f = new File(path + entry.getName());
                    FileOutputStream fos = new FileOutputStream(f);
                    InputStream is = zipFile.getInputStream(entry);
                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    while ((bytesRead = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, bytesRead);
                    }
                    fos.close();
                    System.out.println("Create File " + entry.getName());
                }
            }
        }
    

    那么,明白了。

    【讨论】:

      【解决方案2】:

      您正在为 epub 文件中的每个文件创建一个具有相同名称的 FileOutputStream

      如果文件存在并且它是一个目录,则抛出FileNotFoundException。第一次通过循环时,会创建目录,下一次会引发异常。

      如果你想改变解压 epub 的目录,你应该这样做:

      FileOutputStream fos = new FileOutputStream("/Users/yelinaung/Documents/unzip/xml/" + entry.getName())
      

      更新
      之前创建文件夹以提取我能够打开 epub 文件的每个文件

      ...
      byte data[] = new byte[BUFFER];
      
      String path = entry.getName();
      if (path.lastIndexOf('/') != -1) {
          File d = new File(path.substring(0, path.lastIndexOf('/')));
          d.mkdirs();
      }
      
      // write the files to the disk
      FileOutputStream fos = new FileOutputStream(path);
      ...
      

      要更改提取文件的文件夹,只需更改定义路径的行

      String path = "newFolder/" + entry.getName();
      

      【讨论】:

      • eahh.. 我试过了.. 又遇到了同样的错误.. 它可以提取文件.. 但不能文件夹.. META-INF 被提取为文件。 . 实际上,它是 .epub 文件的子文件夹。
      【解决方案3】:

      这个方法对我来说很好用,

      public void doUnzip(String inputZip, String destinationDirectory)
              throws IOException {
          int BUFFER = 2048;
          List zipFiles = new ArrayList();
          File sourceZipFile = new File(inputZip);
          File unzipDestinationDirectory = new File(destinationDirectory);
          unzipDestinationDirectory.mkdir();
      
          ZipFile zipFile;
          // Open Zip file for reading
          zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
      
          // Create an enumeration of the entries in the zip file
          Enumeration zipFileEntries = zipFile.entries();
      
          // Process each entry
          while (zipFileEntries.hasMoreElements()) {
              // grab a zip file entry
              ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
      
              String currentEntry = entry.getName();
      
              File destFile = new File(unzipDestinationDirectory, currentEntry);
      
              if (currentEntry.endsWith(".zip")) {
                  zipFiles.add(destFile.getAbsolutePath());
              }
      
              // grab file's parent directory structure
              File destinationParent = destFile.getParentFile();
      
              // create the parent directory structure if needed
              destinationParent.mkdirs();
      
              try {
                  // extract file if not a directory
                  if (!entry.isDirectory()) {
                      BufferedInputStream is =
                              new BufferedInputStream(zipFile.getInputStream(entry));
                      int currentByte;
                      // establish buffer for writing file
                      byte data[] = new byte[BUFFER];
      
                      // write the current file to disk
                      FileOutputStream fos = new FileOutputStream(destFile);
                      BufferedOutputStream dest =
                              new BufferedOutputStream(fos, BUFFER);
      
                      // read and write until last byte is encountered
                      while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                          dest.write(data, 0, currentByte);
                      }
                      dest.flush();
                      dest.close();
                      is.close();
                  }
              } catch (IOException ioe) {
                  ioe.printStackTrace();
              }
          }
          zipFile.close();
      
          for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
              String zipName = (String)iter.next();
              doUnzip(
                  zipName,
                  destinationDirectory +
                      File.separatorChar +
                      zipName.substring(0,zipName.lastIndexOf(".zip"))
              );
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-02
        • 1970-01-01
        • 1970-01-01
        • 2014-03-28
        • 1970-01-01
        相关资源
        最近更新 更多