gpt4 book ai didi

java - 无法解压 EPub 文件

转载 作者:行者123 更新时间:2023-12-02 08:33:42 26 4
gpt4 key购买 nike

IMO,我认为 epub是 zipper 的一种。因此,我尝试在 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 的方法正确吗? ..纠正我的错误

最佳答案

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

我已经按照这个方法改正了。

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

然后,明白了。

关于java - 无法解压 EPub 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3147794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com