Reputation: 389
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
Upvotes: 0
Views: 393
Reputation: 351
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();
}
Upvotes: 1
Reputation: 2405
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
Upvotes: 1
Reputation: 389
Restarted my pc, and re-ran the code from cmd and it worked fine :)
Upvotes: 1