android 压缩文件

本篇文章主要记录下android中压缩文件的工具类.
实现压缩功能主要用到 ZipOutputStream和ZipEntry

1: ZipOutputStream

ZipOutputStream类位于java.util.zip包中,它是OutputStream的子类,因此可以像操作其他输出流一样使用它。
使用ZipOutputStream时,我们需要先创建一个ZipEntry对象来表示要添加到ZIP文件中的每个文件或目录,然后使用putNextEntry方法将其添加到ZIP文件中。接着,我们可以使用write方法将数据写入ZIP文件,并使用closeEntry方法关闭当前条目。最后,使用finish方法完成ZIP文件的创建。
ZipOutputStream的一些常用方法:

putNextEntry(ZipEntry entry):将一个新的条目添加到ZIP文件中。
write(byte[] buffer, int offset, int length):将指定字节数组的一部分写入ZIP文件。
closeEntry():关闭当前条目。
finish():完成ZIP文件的创建。
setLevel(int level):设置压缩级别。
setMethod(int method):设置压缩方法。
setComment(String comment):设置ZIP文件的注释。

2: ZipEntry

每个ZipEntry对象代表一个文件或目录,包含了文件名、压缩和解压缩相关的属性信息。
ZipEntry类的常用方法包括:

getName():获取条目的名称。
getSize():获取条目的未压缩大小。
getCompressedSize():获取条目的压缩大小。
isDirectory():判断条目是否为目录。
getLastModifiedTime():获取条目的最后修改时间。
getMethod():获取条目的压缩方法。
getComment():获取条目的注释信息。

public class ZipUtils {
public static boolean zip(String sourceDir, String zipFile, int level) {
if (TextUtils.isEmpty(sourceDir) || TextUtils.isEmpty(zipFile) ) {
throw new RuntimeException(“invalid arguments”);
}

boolean ret = false;
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
zos.setLevel(level);

File file = new File(sourceDir);
String sourcePath;
if (file.isDirectory()) {
sourcePath = file.getPath();
} else {
//直接压缩单个文件时,取父目录
sourcePath = file.getParent();
}

byte[] buffer = new byte[4096];
ret = doZip(file, sourcePath, zos, buffer);
zos.closeEntry();
} catch (Throwable e) {
Log.e(TAG, "zip: ",e );
} finally {
closeQuietly(zos);
}
return ret;
}

public static void closeQuietly(Closeable c) {
if (null != c) {
try {
c.close();
} catch (Throwable e) {
Log.e(TAG, "closeQuietly: ",e );
}
}
}

private static boolean doZip(File source, String basePath, ZipOutputStream zos, byte[] buffer) {
File[] files;
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}

boolean ret = true;
int length;
String pathName;//存相对路径(相对于待压缩的根目录)
try {
for (File file : files) {
pathName = file.getPath().substring(basePath.length() + 1);
if (file.isDirectory()) {
pathName = pathName + “/”;
zos.putNextEntry(new ZipEntry(pathName));
if (!doZip(file, basePath, zos, buffer)) break;
} else {
InputStream is = null;
try {
is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
} catch (Throwable e) {
e.printStackTrace();
ret = false;
break;
} finally {
closeQuietly(is);
}
}
}
} catch (Throwable e) {
Log.e(TAG, "doZip: ",e );
ret = false;
}
return ret;
}
}

调用程序:

ZipUtils.zip("sdcard/test","sdcard/test2.zip",9);
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值