我有这个更新程序类,通过解压缩zip文件来更新我的程序。它工作得很好,除非我添加一个文件夹以便提取..我已经包含了我用来完成这个的3个方法,但问题必须在unzip()
方法中。
打印Extracting: New folder/TEST2.txt
后,会在下面发布错误。我试过制作没有空格的文件夹,它会抛出相同的错误。 zip文件的竞赛只有2个文本文件和一个文件夹。
zip的结构:
新文件夹> Test2.txt
个Test1.txt
错误:
java.io.IOException:系统找不到指定的路径 java.io.WinNTFileSystem.createFileExclusively(Native Method)at java.io.File.createNewFile(未知来源)at update.Main_Gui.unzip(Main_Gui.java:244)at update.Main_Gui.access $ 3(Main_Gui.java:230)at update.Main_Gui $ 3.run(Main_Gui.java:108)at java.lang.Thread.run(未知来源)
代码:
private void copyFiles(File f, String dir) throws IOException {
File[] files = f.listFiles();
for (File ff : files) {
if (ff.isDirectory()) {
new File(dir + "/" + ff.getName()).mkdir();
copyFiles(ff, dir + "/" + ff.getName());
} else {
copy(ff.getAbsolutePath(), dir + "/" + ff.getName());
}
}
}
public void copy(String srFile, String dtFile)
throws FileNotFoundException, IOException {
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
private void unzip() throws IOException {
int BUFFER = 2048;
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile("update.zip");
Enumeration e = zipfile.entries();
(new File(root)).mkdir();
while (e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
outText.setText(outText.getText() + "\nExtracting: " + entry);
if (entry.isDirectory())
(new File(root + entry.getName())).mkdir();
else {
(new File(root + entry.getName())).createNewFile();
is = new BufferedInputStream(zipfile.getInputStream(entry));
int count;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(root
+ entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
//zipfile.close();
dest.flush();
dest.close();
is.close();
}
}
}