使用JAVA ZipFile类解压Zip文件
我正在尝试使用JAVA解压文件,它正在编译,没有任何错误。 当我从我的工具调用它,并给出文件的绝对目标路径和源路径时,例如: 来源:D:\data\test.zip 目的地:D:\data\op\ 我收到错误,如访问被拒绝(我有系统的管理员访问权限) 堆栈跟踪: 提取:测试/新文本文档-复制(2).txt java.io.FileNotFoundException:D:\Data\Op(访问被拒绝)位于 java.io.FileOutputStream.open(本机方法)位于 FileOutputStream.(FileOutputStream.java:179)位于 FileOutputStream.java.io.FileOutputStream。(FileOutputStream.java:70) 下面是我正在调用的函数,我相信它与目的地有关,因为它可能无法提取到绝对路径,但某些临时文件夹无法写入。我在目的地尝试了一些组合,但在我的目的地没有工作。请指导我如何修复它使用JAVA ZipFile类解压Zip文件,java,zip,unzip,gunzip,Java,Zip,Unzip,Gunzip,我正在尝试使用JAVA解压文件,它正在编译,没有任何错误。 当我从我的工具调用它,并给出文件的绝对目标路径和源路径时,例如: 来源:D:\data\test.zip 目的地:D:\data\op\ 我收到错误,如访问被拒绝(我有系统的管理员访问权限) 堆栈跟踪: 提取:测试/新文本文档-复制(2).txt java.io.FileNotFoundException:D:\Data\Op(访问被拒绝)位于 java.io.FileOutputStream.open(本机方法)位于 FileOutp
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();
}
}
您正在尝试写入目录
FileOutputStream fos=新的FileOutputStream(destDir)代码>
试着把它改成
FileOutputStream fos=newfileoutputstream(destDir+File.separator+entry.getName())代码>
使用ZipFile条目
的名称从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();
}
}
}
zip文件的内容可以是目录和文件。提取时,如果是文件,则可以使用.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();
}
您是否从命令行运行应用程序?然后启动cmd
,就像AdminInitrator我从一个工具中调用它一样,这个工具使用admin previllages运行:)我刚刚试过FileOutputStream fos=newFileOutputStream(destDir+File.separator+flName);当文件名在flName peramater中传递时,令人惊讶的是,我得到了压缩文件而不是解压缩文件。我所要做的就是将所有文件从压缩文件写入目标目录。为您更新了我的答案+1以获得完美的代码,最后一个问题,在提取文件时,首先我得到了文件未发现异常。原因是当我们解压文件时,解压查找粗体E:\data\test*bold*之类的内容,结果失败。有什么办法可以避免它和只读子文件,并写入destinatio文件夹?对不起,我不理解你的问题。我会尝试代码,然后再作为答案。