[英]Unzip Zip File Using JAVA ZipFile Class
我正在尝试使用JAVA解压缩文件,并且正在编译而没有任何错误。 当我从工具中调用它时,并给出文件的绝对目标路径和源路径,例如:源:D:\\ data \\ test.zip目标:D:\\ data \\ op \\
我收到像Acess被拒绝一样的错误(我具有系统的Admin访问权限)
堆栈跟踪:
提取:测试/新文本文档-复制(2).txt java.io.FileNotFoundException:D:\\ Data \\ Op(拒绝访问),位于java.io.FileOutputStream.open(本机方法),位于java.io.FileOutputStream。 (FileOutputStream.java:179),位于java.io.FileOutputStream。(FileOutputStream.java:70)
下面是我正在调用的函数,我认为它与目标有关,因为它可能会提取到绝对路径,但会提取一些无法写入的临时文件夹。 我在目的地尝试了一些组合,但没有达到目的。请指导我如何解决。
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();
}
}
压缩文件的内容可以是目录,也可以是文件。 提取时,如果ZipEntry是文件,则可以使用BufferedOutputStream .write()以直接方式提取它。 但是,当ZipEntry是目录时,您将无法执行此操作,因为不存在目录。 因此,您必须使用路径创建一个新目录。
上面的代码没有考虑目录,因此必须写入新目录的文件会引发问题中提到的异常。
下面的方法可以解决您的问题。
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();
// iterates over entries in the zip file
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
您正在尝试写入目录
FileOutputStream fos = new FileOutputStream(destDir);
尝试将其更改为
FileOutputStream fos = new FileOutputStream(destDir + File.separator + entry.getName ());
使用ZipFile entry
的名称
Oracle提供的一个简单的解压缩 示例 :
import java.io.*;
import java.util.zip.*;
public class UnZip {
final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedOutputStream dest = null;
FileInputStream fis = new
FileInputStream(argv[0]);
ZipInputStream zis = new
ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
FileOutputStream fos = new
FileOutputStream(entry.getName());
dest = new
BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER))
!= -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.