2016-05-20 277 views
0

我目前正试图创建一个Java程序,将文件夹解压到某个文件夹中的文件夹Program Files内的程序文件夹中。解压缩文件夹中使用Java

我使用的第三方库Zip4j解压缩的文件夹。以下是我正在使用的代码。

String source = "C:\\Users\\chris\\Desktop\\New folder.zip"; 
    String destination = "C:\\Program Files (x86)\\Test Folder"; 
    String password = "password"; 

    try { 
     ZipFile zipFile = new ZipFile(source); 
     if (zipFile.isEncrypted()) { 
      zipFile.setPassword(password); 
     } 
     zipFile.extractAll(destination); 
    } catch (ZipException e) { 
     e.printStackTrace(); 
    } 

它工作完美,如果我想解压到桌面上的普通文件夹。但是一旦我尝试将其解压缩到程序文件,我会收到以下Exception java.io.FileNotFoundException

我认为我的程序需要管理员权限才能访问Program Files文件夹中的文件夹。有谁知道如何做到这一点?

错误日志:

Caused by: java.io.FileNotFoundException: C:\Program Files (x86)\Test  Folder\New folder\New Text Document.txt (The system cannot find the path specified) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.<init>(FileOutputStream.java:213) at java.io.FileOutputStream.<init>(FileOutputStream.java:162) at net.lingala.zip4j.unzip.UnzipEngine.getOutputStream(UnzipEngine.java:432) ... 7 more 

回答

1

请尝试检查如果文件夹访问或存在。另外如果你有权限的文件夹。然后试试下面的代码:

String source = "C:\\Users\\chris\\Desktop\\New folder.zip"; 
String destination = "C:\\Program Files (x86)\\Test Folder"; 
String password = "password"; 

try { 
    ZipFile zipFile = new ZipFile(source); 
    if (zipFile.isEncrypted()) { 
     zipFile.setPassword(password); 
    } 
    File file = new File(destination); 
    if (file.exists()) { 
     zipFile.extractAll(destination); 
    } else { 
     System.out.println("Foolder not exists"+destination); 
    } 
} catch (ZipException e) { 
    e.printStackTrace(); 
} 
+0

文件夹确实存在,因为我创造了自己。但是它似乎是一个许可的事情,因为当代码无法打开路径名中表示的文件夹/文件名时引发异常。我试过你的代码,但是我得到了和以前一样的错误。 – noobCoder

+0

你能发布错误日志吗? – mattymanme

+0

产生的原因:java.io.FileNotFoundException:C:\ Program Files文件(x86)的\ Test文件夹\新建文件夹\新建文本文档(该系统找不到指定的路径) \t在java.io.FileOutputStream中。 openO(Native Method) \t at java.io.FileOutputStream.open(FileOutputStream.java:270) \t at java.io.FileOutputStream。 (FileOutputStream.java:213) \t at java.io.FileOutputStream。 (FileOutputStream.java:162) \t在net.lingala.zip4j.unzip.UnzipEngine.getOutputStream(UnzipEngine.java:432) \t ... 7更 – noobCoder

1

重新启动我的电脑,再跑到离CMD的代码,它工作得很好:)

+0

good:)................ – mattymanme