在Android中解压缩文件 [英] Unzip file in Android
本文介绍了在Android中解压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了以下错误,当我尝试解压缩zip文件
I got following error when i try to unzip zip file
结束的中央目录签字找不到
End-of-central-directory signature not found
我也曾经尝试7zip的lib中,它工作在简单的Java,但Android平台的罚款。结果
我得到一个依赖的jar未找到错误。
I have also try 7zip lib, it works fine in simple java but in android platform.
I get a "dependent jar not found" error.
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");
}
//文件头总是给误差
//File header always give error
推荐答案
在Android的解压文件时,不需要第三方库。看看这个:
You don't need 3rd party libraries when unzipping files in Android. Have a look at this:
的http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29
这篇关于在Android中解压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文