Reputation: 3351
I'm trying to create and add files to that directory. And below is my code, I'm able to create and add files to them, but at the end it is throwing me an exception. My Code:
Unzip_Main.java
import java.io.File;
public class UnZip_Main {
public static void main(String[] args) {
String zipFilePath = "D:\\News\\Zip\\";
String destDirectory = "D:\\News\\Zip\\Result\\";
new File(destDirectory).mkdir();
UnZip unzipper = new UnZip();
File dir = new File(zipFilePath);
File[] files = dir.listFiles();
if (null != files) {
for (int fileIntList = 0; fileIntList < files.length; fileIntList++) {
String ss = files[fileIntList].toString();
if (null != ss && ss.length() > 0) {
System.out.println("unzip path is ");
try {
System.out.println("dest directry is " + destDirectory);
unzipper.unzip(zipFilePath + ss.substring(ss.lastIndexOf("\\") + 1, ss.length()),
destDirectory);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
}
Unzip.java
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UnZip {
private static final int BUFFER_SIZE = 4096;
public void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath, zipFilePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
private void extractFile(ZipInputStream zipIn, String filePath, String zipFilePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath, true));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
File newName = new File(filePath);
String str = zipFilePath.substring(zipFilePath.lastIndexOf("\\") + 1, zipFilePath.lastIndexOf("."));
File zipPath = new File(filePath);
zipPath.mkdir();
File oldName = new File(zipPath.getParent() + "\\" + str + ".xml");
if (oldName.exists()) {
oldName.delete();
}
System.out.println("new name is " + newName + "and old name is " + oldName);
if (newName.renameTo(oldName)) {
System.out.println("Renamed");
} else {
System.out.println("Not Renamed");
}
}
}
My output:
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If16c0c30613111e5850ddea403ecf0ba.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If83120c05dd311e599a896be76e2f024.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If8915610629d11e5b64da6abc0693b3d.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If93445c0661f11e5839c9a236dd16599.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\If9bd10a061f411e5b445d6756f17230b.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\Ife581970612c11e5b64da6abc0693b3d.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\Ifed1c1f05f9a11e5bc448d3219668f6c.xml
Renamed
unzip path is
dest directry is D:\News\Zip\Result\
new name is D:\News\Zip\Result\content.xmland old name is D:\News\Zip\Result\Iff5aa9905d4011e5bb1df062954439f5.xml
Renamed
Exception at the end:
java.io.FileNotFoundException: D:\News\Zip\Result (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at UnZip.unzip(UnZip.java:17)
at UnZip_Main.main(UnZip_Main.java:19)
The files are created in correct folder, all of them are created, but still getting this exception, unable to know where I have gone wrong and how to fix it.
Another thing observed is if i change String destDirectory = "D:\\News\\Zip\\Result\\";
to String destDirectory = "D:\\News\\Zip\\";
, and if there is any folder inside Zip
path, i'm getting the same above result with exception, else it is not throwing any exception.
Upvotes: 2
Views: 166
Reputation: 56
add a check for like this
if(files[fileIntList].isDirectory()) continue;
also you should change your file rename code it should be
oldName.renameTo(newName)
ideally you should remove new File(destDirectory).mkdir();
Upvotes: 2
Reputation: 533510
You can't open a directory as a file. Your FileInputStream
is attempting to open D:\\News\\Zip\\Result\\
which is a directory.
Upvotes: 1
Reputation: 1567
This line is causing the exception:
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
because you are giving it a directory ( D:\News\Zip\Result )
please make sure you are giving it a file path and not a directory path.
Try to put a breakpoint to check.
Get FileNotFoundException when initialising FileInputStream with File object
Upvotes: 1