当我尝试解压 zip 文件时出现以下错误
End-of-central-directory signature not found
我也尝试过 7zip lib,它在简单的 java 中运行良好,但在 android 平台上运行良好。
我收到“未找到相关 jar”错误。
try {
// Initiate the ZipFile
ZipFile zipFile = new ZipFile(file);
String destinationPath = destPath;
// If zip file is password protected then set the password
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
//Get a list of FileHeader. FileHeader is the header information for all the
//files in the ZipFile
List fileHeaderList = zipFile.getFileHeaders();
// Loop through all the fileHeaders
for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
if (fileHeader != null) {
//Build the output file
String outFilePath = destinationPath + System.getProperty("file.separator") + fileHeader.getFileName();
File outFile = new File(outFilePath);
//Checks if the file is a directory
if (fileHeader.isDirectory()) {
//This functionality is up to your requirements
//For now I create the directory
outFile.mkdirs();
continue;
}
//Check if the directories(including parent directories)
//in the output file path exists
File parentDir = outFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
//Get the InputStream from the ZipFile
is = zipFile.getInputStream(fileHeader);
//Initialize the output stream
os = new FileOutputStream(outFile);
int readLen = -1;
byte[] buff = new byte[BUFF_SIZE];
//Loop until End of File and write the contents to the output stream
while ((readLen = is.read(buff)) != -1) {
os.write(buff, 0, readLen);
}
//Please have a look into this method for some important comments
closeFileHandlers(is, os);
//To restore File attributes (ex: last modified file time,
//read only flag, etc) of the extracted file, a utility class
//can be used as shown below
UnzipUtil.applyFileAttributes(fileHeader, outFile);
System.out.println("Done extracting: " + fileHeader.getFileName());
} else {
System.err.println("fileheader is null. Shouldn't be here");
}
//文件头总是报错
最佳答案
在 Android 中解压缩文件时不需要第 3 方库。看看这个:
http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29
关于android - 在 Android 中解压文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8295496/