Android压缩文件访问总结
Android压缩文件也是可以通过代码来访问到的,这里使用到两个Zip压缩类,并且只能读取到Zip类型的压缩文件。也可以对文件进行解压,其实就是读取后再写出来。
直接提供代码,都是基于java中IO流的知识了:
一.获取压缩文件内的文件名称
//扫描压缩文件里面的文件名字,传人的是压缩文件的决定路径
public static void scanZipFile(String zipname) {
try {
ZipInputStream zin = new ZipInputStream(
new FileInputStream(zipname));
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
//压缩文件里面的文件名称
System.out.println(entry.getName());
zin.closeEntry();
}
zin.close();
} catch (IOException e) {
}
}
二.获取压缩文件里某一个文件的数据
//扫描压缩文件里面的某一个文件的数据,传人的是压缩文件的决定路径和里面的某一个文件的名称
public void scanZipFile(String zipname, String name) {
Log.e("TAG", "scanZipFile");
try {
ZipInputStream zin = new ZipInputStream(
new FileInputStream(zipname));
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (entry.getName().equals(name)) {
StringBuffer stringBuffer = new StringBuffer();
int len = 0;
byte[] buf = new byte[1024];
while ((len = zin .read(buf)) != -1) {
stringBuffer.append(new String(buf, 0, len));
}
Log.e("TAG", "stringBuffer:" + stringBuffer);
}
zin.closeEntry();
}
zin.close();
} catch (IOException e) {
Log.e("TAG", e.getMessage());
}
三.解压文件的代码
/*
* 这个是解压ZIP格式文件的方法
*
* @zipFileName:是传进来你要解压的文件路径,包括文件的名字;
*
* @outputDirectory:选择你要保存的路劲;
*
*/
public static void unzip(String zipFileName, String outputDirectory)
throws Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
Log.e("TAG", "in==null"+(in==null));
ZipEntry z;
String name = "";
String extractedFile = "";
int counter = 0;
while ((z = in.getNextEntry()) != null) {
name = z.getName();
Log.d("TAG", "unzipping file: " + name);
if (z.isDirectory()) {
Log.d("TAG", name + "is a folder");
// get the folder name of the widget
name = name.substring(0, name.length() - 1);
File folder = new File(outputDirectory + File.separator + name);
folder.mkdirs();
if (counter == 0) {
extractedFile = folder.toString();
}
counter++;
Log.d("TAG", "mkdir " + outputDirectory + File.separator + name);
} else {
Log.d("TAG", name + "is a normal file");
File file = new File(outputDirectory + File.separator + name);
file.createNewFile();
// get the output stream of the file
FileOutputStream out = new FileOutputStream(file);
int ch;
byte[] buffer = new byte[1024];
// read (ch) bytes into buffer
while ((ch = in.read(buffer)) != -1) {
// write (ch) byte from buffer at the position 0
out.write(buffer, 0, ch);
out.flush();
}
out.close();
}
}
in.close();
}
上面三个方法都是测试成功的。失败的话只能是路径不对或没添加权限。
但是第三个方法好像不能解压大文件,
如果是大文件可以使用下面的方法:
四.解压大文件的代码
/**
* 2 * 解压缩功能.
* 3 * 将zipFile文件解压到folderPath目录下.
* 4 * @throws Exception
* 5
*/
public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
//public static void upZipFile() throws Exception{
ZipFile zfile = new ZipFile(zipFile);
Enumeration zList = zfile.entries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
Log.d("upZipFile", "ze.getName() = " + ze.getName());
String dirstr = folderPath + ze.getName();
//dirstr.trim();
dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
Log.d("upZipFile", "str = " + dirstr);
File f = new File(dirstr);
f.mkdir();
continue;
}
Log.d("upZipFile", "ze.getName() = " + ze.getName());
OutputStream os = new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
Log.d("upZipFile", "writing");
}
is.close();
os.close();
}
zfile.close();
Log.d("upZipFile", "finish");
return true;
}
public static File getRealFileName(String baseDir, String absFileName) {
String[] dirs = absFileName.split("/");
String lastDir = baseDir;
if (dirs.length > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
lastDir += (dirs[i] + "/");
File dir = new File(lastDir);
if (!dir.exists()) {
dir.mkdirs();
Log.d("getRealFileName", "create dir = " + (lastDir + "/" + dirs[i]));
}
}
File ret = new File(lastDir, dirs[dirs.length - 1]);
Log.d("upZipFile", "2ret = " + ret);
return ret;
} else {
return new File(baseDir, absFileName);
}
}
上面这段代码是可以解压几百兆的压缩文件的,已经试过了。
要添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>