我必须使用哪个库来处理 android 上的档案(如 rar、zip)。一些示例代码。我找不到任何存档文件的示例。
问问题
6789 次
4 回答
2
你是拉拉链还是解压?(或两者兼而有之?)以前我使用过ZipInputStream。您使用的策略可能取决于 Zip 的存储位置(在 SD 卡上/在 APK 的资产中/在 APK 扩展中)。例如,如果它是一项资产,您可以使用 AssetManager 将文件作为 InputSteam 打开。如果它在 SD 卡上,您可能需要使用ZipFile。
这里有一个 Java 教程可能会有所帮助:http: //java.sun.com/developer/technicalArticles/Programming/compression/
于 2012-03-20T09:43:54.797 回答
1
看这个例子
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
System.out.println("^^^^^^UnzippingFile^"+ze.getName());
///code to search is given string exists or not in a Sentence
String haystack = ze.getName();
String needle1 = ".DS_Store";
int index1 = haystack.indexOf(needle1);
if (index1 != -1)
{
System.out.println("The string contains the substring "
+ needle1);
continue;
}
/*else
System.out.println("The string does not contain the
substring " + needle1);*/
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location +
ze.getName());
// replace for loop with:
byte[] buffer = new byte[1024];
int length;
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zin.closeEntry();
fout.close();
}
}////Outer While
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
于 2012-03-20T09:40:58.700 回答
0
对于 zip 你可以使用java.util.Zip,对于 rar 存档我认为你必须通过第三方库
于 2012-03-20T09:40:33.380 回答
-4
右键单击您的项目---> Properties --->Java Build path ----> Libraries---> Add External Jar 然后以任何格式 .zip 或 .rar 在您的项目中添加库
于 2012-03-20T09:41:06.453 回答