3

IMO,我认为epub是一种 zip 。因此,我尝试以某种方式解压缩。

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的方式是正确的方式吗?.. 纠正我

4

3 回答 3

5

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

我以这种方式更正了。

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());
            }
        }
    }

然后,明白了。

于 2010-07-02T04:02:34.220 回答
3

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

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();
于 2010-07-01T03:31:06.260 回答
1

这个方法对我很好,

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"))
        );
    }

}
于 2012-07-02T05:56:03.593 回答