public void unZipFile(String zipFileLocation, String outputFolder) {
logger.info("ZipFileLocation: "+zipFileLocation);
logger.info("OutputLocation: "+outputFolder);
File dir = new File(outputFolder);
// create output directory if it doesn't exist
if(!dir.exists()) dir.mkdirs();
FileInputStream fis;
//buffer for read and write data to file
byte[] buffer = new byte[1024];
try {
fis = new FileInputStream(zipFileLocation);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
while(ze != null){
String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);
System.out.println("Unzipping to "+newFile.getAbsolutePath());
//create directories for sub directories in zip
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
//close this ZipEntry
zis.closeEntry();
ze = zis.getNextEntry();
}
//close last ZipEntry
zis.closeEntry();
zis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我用来将文件夹解压缩到特定位置的代码。我执行此过程后会遇到异常。请告知为什么会出现这个问题?
java.util.zip.ZipException: invalid entry size (expected 3173388 but got 3173359 bytes)
[java] at java.util.zip.ZipInputStream.readEnd(ZipInputStream.java:403)
[java] at java.util.zip.ZipInputStream.read(ZipInputStream.java:195)
[java] at java.io.FilterInputStream.read(FilterInputStream.java:107)
[java] at com.shipxpress.irf.server.service.impl.IrfServiceImpl.unZipFile(IrfServiceImpl.java:1020)
[java] at com.shipxpress.irf.server.service.impl.IrfServiceImpl.executeFileTransferProcess(IrfServiceImpl.java:1310)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:606)
真的很感激,如果有人可以帮我解决这个问题
谢谢,
答案 0 :(得分:1)
使用此库www.lingala.net/zip4j /
将此jar文件添加到app的lib文件夹中。
检查您的导入
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
使用以下方法
解压缩(" /sdcard/file.zip"," / SD卡/ unzipFolder&#34)
public static void unzip(String Filepath, String DestinationFolderPath) {
try {
ZipFile zipFile = new ZipFile(Filepath);
List fileHeaders = zipFile.getFileHeaders();
for(int i=0;i<fileHeaders.size();i++) {
FileHeader fileHeader=(FileHeader) fileHeaders.get(i);
String fileName = fileHeader.getFileName();
Log.d(TAG,fileName);
if (fileName.contains("\\")) {
fileName=fileName.replace("\\","\\\\");
String[] Folders=fileName.split("\\\\");
StringBuilder newFilepath=new StringBuilder();
newFilepath.append(DestinationFolderPath);
for (int j=0;j<Folders.length-1;j++){
newFilepath.append(File.separator);
newFilepath.append(Folders[j]);
}
zipFile.extractFile(fileHeader, newFilepath.toString(),null,Folders[Folders.length-1]);
}else {
zipFile.extractFile(fileHeader,DestinationFolderPath);
}
}
} catch (ZipException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
我不知道为什么它在你的情况下失败(无效的zip文件?),但我建议:
答案 2 :(得分:0)
我能想到这些原因:
答案 3 :(得分:0)
而不是将其硬编码为
byte[] buffer = new byte[1024];
请尝试以下。
byte[] buffer = new byte[(int) ze.getSize()];
您可以在此处找到详细的教程。
答案 4 :(得分:0)
我遇到了类似的问题,我修改了代码以使其正常工作,结果发现该代码无法区分文件和文件夹,而这就是我添加的检查。
public static void unzipFile(String zipFilePath, String destDir)
{
File dir = new File(destDir);
// create output directory if it doesn't exist
if (!dir.exists())
dir.mkdirs();
FileInputStream fis;
// buffer for read and write data to file
byte[] buffer = new byte[1024];
try
{
fis = new FileInputStream(zipFilePath);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
while (ze != null)
{
try
{
String fileName = ze.getName();
File newFile = new File(destDir + File.separator + fileName);
System.out.println("Unzipping to " + newFile.getAbsolutePath());
// create directories for sub directories in zip
new File(newFile.getParent()).mkdirs();
if(ze.isDirectory()) // check if this is a diectory or file
{
newFile.mkdirs();
}
else
{
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0)
{
fos.write(buffer, 0, len);
}
fos.close();
}
// close this ZipEntry
zis.closeEntry();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
ze = zis.getNextEntry();
}
// close last ZipEntry
zis.closeEntry();
zis.close();
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}