作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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)
如果有人能帮助我解决这个问题,我真的很感激
谢谢
最佳答案
使用这个库www.lingala.net/zip4j/
将此 jar 文件添加到应用程序的 lib 文件夹中。
像这样检查您的导入
导入net.lingala.zip4j.core.ZipFile;
导入net.lingala.zip4j.exception.ZipException;
导入net.lingala.zip4j.model.FileHeader;
使用下面的方法
解压缩(“/sdcard/file.zip”,“/sdcard/unzipFolder”)
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();
}
}
关于java - 使用java解压文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42509675/
我是一名优秀的程序员,十分优秀!