gpt4 book ai didi

java - 使用java解压文件

转载 作者:行者123 更新时间:2023-12-01 08:48:54 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com