java提取Zip文件
我正在寻找一种提取Zip文件的方法。到目前为止,我已经尝试了java.util.zip和org.apache.commons.compress,但都给出了损坏的输出 基本上,输入是一个包含一个.doc文件的ZIP文件 java.util.zip:输出已损坏。 org.apache.commons.compress:输出空白文件,但大小为2MB 到目前为止,只有像Winrar这样的商业软件能够完美地工作。有没有一个java库可以利用它 这是我使用java.util库的方法:java提取Zip文件,java,zip,extract,Java,Zip,Extract,我正在寻找一种提取Zip文件的方法。到目前为止,我已经尝试了java.util.zip和org.apache.commons.compress,但都给出了损坏的输出 基本上,输入是一个包含一个.doc文件的ZIP文件 java.util.zip:输出已损坏。 org.apache.commons.compress:输出空白文件,但大小为2MB 到目前为止,只有像Winrar这样的商业软件能够完美地工作。有没有一个java库可以利用它 这是我使用java.util库的方法: public void
public void extractZipNative(File fileZip)
{
ZipInputStream zis;
StringBuilder sb;
try {
zis = new ZipInputStream(new FileInputStream(fileZip));
ZipEntry ze = zis.getNextEntry();
byte[] buffer = new byte[(int) ze.getSize()];
FileOutputStream fos = new FileOutputStream(this.tempFolderPath+ze.getName());
int len;
while ((len=zis.read(buffer))>0)
{
fos.write(buffer);
}
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
if (zis!=null)
{
try { zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
非常感谢,,
迈克我想你的输入可能被一些“不兼容”的zip程序压缩了,比如7zip。 试着首先调查一下它是否可以用经典的WinZip或类似的软件解包
JavasZIP处理非常能够处理来自“兼容”zip压缩器的压缩档案。我认为您的输入可能被一些“不兼容”的zip程序(如7zip)压缩。 试着首先调查一下它是否可以用经典的WinZip或类似的软件解包
JavasZIP处理非常能够处理来自“兼容”zip压缩器的压缩档案。您试过jUnrar吗?也许它会起作用:
如果这样也不行,我想你的档案在某种程度上被破坏了。你试过jUnrar吗?也许它会起作用:
如果这也不起作用,我想你的档案在某种程度上被破坏了。这是我的代码中的一个错误。我需要指定写入字节的偏移量和长度。这是我的代码中的一个错误。我需要指定写入字节的偏移量和长度。如果您知道要在其中运行此代码的环境,我认为您最好只需调用系统为您解压。它将比用java实现的任何东西都要快 我编写代码来提取一个带有嵌套目录的zip文件,它运行缓慢,占用了大量CPU。最后我用这个来代替它:
Runtime.getRuntime().exec(String.format("unzip %s -d %s", archive.getAbsolutePath(), basePath));
这会更好。如果您知道要在其中运行此代码的环境,我认为您最好只需调用系统来为您解压。它将比用java实现的任何东西都要快 我编写代码来提取一个带有嵌套目录的zip文件,它运行缓慢,占用了大量CPU。最后我用这个来代替它:
Runtime.getRuntime().exec(String.format("unzip %s -d %s", archive.getAbsolutePath(), basePath));
那效果好多了。对我来说很好
ZipFile Vanilla = new ZipFile(new File("Vanilla.zip")); //zipfile defined and needs to be in directory
Enumeration<? extends ZipEntry> entries = Vanilla.entries();// all (files)entries of zip file
while(entries.hasMoreElements()){//runs while there is files in zip
ZipEntry entry = entries.nextElement();//gets name of file in zip
File folderw =new File("tkwgter5834");//creates new directory
InputStream stream = Vanilla.getInputStream(entry);//gets input
FileInputStream inpure= new FileInputStream("Vanilla.zip");//file input stream for zip file to read bytes of file
FileOutputStream outter = new FileOutputStream(new File(folderw +"//"+ entry.toString())); //fileoutput stream creates file inside defined directory(folderw variable) by file's name
outter.write(inpure.readAllBytes());// write into files which were created
outter.close();//closes fileoutput stream
}
zipfilevanilla=newzipfile(新文件(“Vanilla.zip”)//zipfile已定义并且需要位于目录中
这对我很有效
ZipFile Vanilla = new ZipFile(new File("Vanilla.zip")); //zipfile defined and needs to be in directory
Enumeration<? extends ZipEntry> entries = Vanilla.entries();// all (files)entries of zip file
while(entries.hasMoreElements()){//runs while there is files in zip
ZipEntry entry = entries.nextElement();//gets name of file in zip
File folderw =new File("tkwgter5834");//creates new directory
InputStream stream = Vanilla.getInputStream(entry);//gets input
FileInputStream inpure= new FileInputStream("Vanilla.zip");//file input stream for zip file to read bytes of file
FileOutputStream outter = new FileOutputStream(new File(folderw +"//"+ entry.toString())); //fileoutput stream creates file inside defined directory(folderw variable) by file's name
outter.write(inpure.readAllBytes());// write into files which were created
outter.close();//closes fileoutput stream
}
zipfilevanilla=newzipfile(新文件(“Vanilla.zip”)//zipfile已定义并且需要位于目录中
Enumeration可能不是Zip文件也可能不是Zip文件WinRAR提取工作。我会尝试一下你的建议,谢谢你的努力。我会尝试一下你的建议,谢谢。我会添加用于提取的代码。说到底,这一切可能都归结于我的java方法。说到底,这一切都可能归结于我的java方法。请为您的答案提供一些上下文。它将帮助其他用户轻松理解答案。@PawaraSiriwardhane请为您的答案提供一些上下文。它将帮助其他用户轻松理解答案。@PawaraSiriwardhane done