无法解压缩EPub文件

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

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

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

我已经用这种方式纠正了。

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

然后,得到它。

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

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

如果要更改提取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(); 

这个方法对我很好,

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