无法解压 EPub 文件
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 的方法是正确的方法吗? .. 纠正我
IMO, I thought that epub is a kind of zip . Thus, I've tried to unzip in 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();
}
}
}
I got that following error ..
java.io.FileNotFoundException: /Users/yelinaung/Documents/unzip/xml (No such file or directory)
though I've created a folder in that way .. and
my way of unzipping epub is right way ? .. Correct Me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经得到答案了..我还没有检查必须提取的对象是否是文件夹。
我已经按照这个方法改正了。
然后,明白了。
I've got the answer.. I haven't checked that the object that have to be extracted is folder or not .
I've corrected in this way .
Then, got it .
您正在为 epub 文件中的每个文件创建一个具有相同名称的
FileOutputStream
。如果文件存在并且是目录,则抛出 FileNotFoundException 。第一次通过循环时,会创建目录,下一次会引发异常。
如果您想更改 epub 的解压目录,您应该执行以下操作:
更新
在提取每个文件之前创建文件夹我能够打开 epub 文件
并更改提取文件的文件夹,只需更改定义路径的行
You are creating a
FileOutputStream
with the same name for every file inside your epub file.The
FileNotFoundException
is thrown if the file exists and it is a directory. The first time you pass through the loop, the directory is created and in the next time, the exception is raised.If you want to change the directory where the epub is extracted, you should do something like this:
Update
Creating the folder before to extract each file I was able to open the epub file
And to change the folder where the files are extracted, just change the line where the path is defined
这个方法对我来说效果很好
This method worked fine for me,