0

I'm currently trying to create a Java program that will unzip a folder into a certain folder within the Program Files Folder.

I'm using the 3rd party library Zip4j to unzip the folder. the following is the code i'm using.

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

it works perfectly if I'm trying to unzip to a normal folder on the desktop. but once i try to unzip it to the program files i get the following Exception java.io.FileNotFoundException.

I assume that my program needs admin rights to be able access the folder within the Program Files folder. Does anyone know how to do this?

Error log:

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
mattyman
  • 351
  • 2
  • 16
noobCoder
  • 370
  • 1
  • 8
  • 18

3 Answers3

1

Please try to check if the folder is accessible or exists. Also if you have permission to the folder. Then Try below code:

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();
}
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
mattyman
  • 351
  • 2
  • 16
  • The folder does exist, as i created myself. but ya it seems to be a permission thing, as that exception is thrown when the code can't open the folder/file name represented on the pathname. I tried your code, but i got the same error as before. – noobCoder May 20 '16 at 10:34
  • 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.(FileOutputStream.java:213) at java.io.FileOutputStream.(FileOutputStream.java:162) at net.lingala.zip4j.unzip.UnzipEngine.getOutputStream(UnzipEngine.java:432) ... 7 more – noobCoder May 20 '16 at 10:52
  • did you get this exception with the new code i provided? pls try that. Also as you said if it is a permission thing try with permissions. – mattyman May 20 '16 at 10:56
  • Ya i got that exception from the code you posted, i ran the jar file from cmd while running in admin and it still didn't work :( – noobCoder May 20 '16 at 10:59
  • this may seem like a noob question but why do i see " C:\Program Files (x86)\Test Folder\New folder\New Text Document.txt " in your logs, instead of "destination = "C:\\Program Files (x86)\\Test Folder"? – mattyman May 20 '16 at 11:08
  • New folder is the name of the zipped folder, and within New folder is the blank text file called new Text Doc, they are the files i want to unzip into the program files folder and the errors states that they can't be written to the test folder. but it is destination = "C:\\Program Files (x86)\\Test Folder" inside my code. – noobCoder May 20 '16 at 11:38
1

Restarted my pc, and re-ran the code from cmd and it worked fine :)

noobCoder
  • 370
  • 1
  • 8
  • 18
1

You need admin rights to write to the program folder. Open the administrator command line and execute from there.

Check here how to enable admin rights from java code: [Run Java file as Administrator with full privileges

Community
  • 1
  • 1
tak3shi
  • 2,305
  • 1
  • 20
  • 33