I am trying to unzip files using JAVA and It is compiling without any errors. When i am calling it from my tool, and giving absolute destination path and source path of file e.g: Source: D:\data\test.zip Destination: D:\data\op\
I am getting Error like Acess is Denied (I have Admin access of system)
Stack trace:
Extracting: test/New Text Document - Copy (2).txt java.io.FileNotFoundException: D:\Data\Op (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.(FileOutputStream.java:179) at java.io.FileOutputStream.(FileOutputStream.java:70)
Below is the function I am calling, I believe it has to do with destination as it may ot extracting to absolute path but some temp folder where it cannot write. I tried some combination on destination but not working form my end.Please guide me how we can fix it.
public void unzip(String zipFilePath, String destDir, String flName) throws Exception
{
int BUFFER = 2048;//Buffer Size
try
{
File dir = new File(destDir);
// Throw Exception if output directory doesn't exist
if(!dir.exists())
{
//Print Message in Consol
System.out.println("No Destination Directory Exists for Unzip Operation.");
throw new Exception();
}
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(zipFilePath);
Enumeration e = zipfile.entries();
while(e.hasMoreElements())
{
entry = (ZipEntry) e.nextElement();
System.out.println("Extracting: " +entry);
is = new BufferedInputStream (zipfile.getInputStream(entry));
int count;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(destDir);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER)) != -1)
{
dest.write(data, 0, count);
}
//Close All Streams
dest.flush();
dest.close();
is.close();
}
}
catch(Exception e)
{
e.printStackTrace();
throw new Exception();
}
}
You are trying to write to the directory
FileOutputStream fos = new FileOutputStream(destDir);
try changing it to
FileOutputStream fos = new FileOutputStream(destDir + File.separator + entry.getName ());
using the ZipFile entry
's name