【发布时间】:2015-11-06 08:27:20
【问题描述】:
我正在尝试解压缩文件,有人建议我使用 codeJava.net 的解压缩实用程序,但我无法让它工作。以下是按下按钮时发生的我的代码的 sn-p。
public void fileSelector(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(new ExtensionFilter("ZIP FILES ONLY", "*.zip"));
File selectedFile = fileChooser.showOpenDialog(primaryStage);
if (selectedFile != null) {
System.out.println(selectedFile);
UnzipUtility unzipper = new UnzipUtility();
String destination = System.getProperty("user.dir");
String finalDestination = destination + "\\books";
System.out.println(finalDestination);
String initialDestination = selectedFile.getPath();
System.out.println(initialDestination);
try {
System.out.println("unzipping ... beep boop beep");
unzipper.unzip(initialDestination, destination);
}
catch (Exception e) {
e.printStackTrace();
}
}
这意味着使用 JavaFX 文件选择器来选择文件,然后在被解压缩对象使用之前将文件路径转换为字符串。您可以在http://www.codejava.net/java-se/file-io/programmatically-extract-a-zip-file-using-java 找到解压缩实用程序。 这是我得到的错误:
java.io.FileNotFoundException: F:\EbookReader\books\New folder\1.txt (The system cannot find the path specified)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
感谢您的帮助。
【问题讨论】:
-
FileNotFoundException: F:\EbookReader\New folder\1.txt (The system cannot find the path specified)您是否检查过该文件是否确实存在于该路径中? -
感谢您的快速回复;p。 EBookReader\books 文件夹存在,但“新文件夹/1.txt 是初始目的地内的内容。解压缩实用程序(我很确定)旨在提取这些文件。
-
您需要使用整个堆栈跟踪。我猜 1.txt 在你的 zip 文件中,它无法写入文件。 “新文件夹”是否存在?
-
ZIP 文件夹中是新文件夹\1.txt。是的,我认为问题在于它无法写入新文件。
-
删除 zip 文件中的所有目录,然后重试。另外,尝试调试
unzip方法,看看到底发生了什么。检查目录中的权限也是一种选择。
标签: java javafx unzip filechooser