package zcom.pekall.backuprestore.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import android.util.Log;
public class TarUtil {
private static final String TAG = TarUtil.class.getName();
private static final int BUFFER = 1024;
private static String mCompressSrcParentPath = "";
/**
* usage: compress("/mnt/sdcard/Download", "/mnt/sdcard/Download.tar");
*
* @param srcAbsolutePath can be path of file or directory
* @param destAbsolutePath suggest ending with a ".tar" suffix
*/
public static void compress(String srcAbsolutePath, String destAbsolutePath) {
compress(new File(srcAbsolutePath), destAbsolutePath);
}
/**
* @param srcFile can be file or directory
* @param destAbsolutePath suggest ending with a ".tar" suffix
*/
public static void compress(File srcFile, String destAbsolutePath) {
File destFile = new File(destAbsolutePath);
try {
if (!destFile.exists()) {
destFile.createNewFile();
}
TarArchiveOutputStream taos = new TarArchiveOutputStream(new FileOutputStream(destFile));
mCompressSrcParentPath = srcFile.getParent();
compress(srcFile, taos);
taos.flush();
taos.close();
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
private static void compress(File file, TarArchiveOutputStream taos) {
if (file.isDirectory()) {
compressDir(file, taos);
} else {
compressFile(file, taos);
}
}
private static void compressDir(File dir, TarArchiveOutputStream taos) {
File[] files = dir.listFiles();
if (files != null) {
if (files.length == 0) {
try {
taos.putArchiveEntry(createTarArchiveEntry(dir));
taos.closeArchiveEntry();
} catch (IOException e) {
Log.e(TAG, e.toString());
}
} else {
for (File file : files) {
compress(file, taos);
}
}
}
}
private static TarArchiveEntry createTarArchiveEntry(File file) {
/** 不能用绝对路径,举例对于压缩/mnt/sdcard下的abc文件,如果不做截短,压缩文件里面会在abc外面套mnt和sdcard两层空壳,所以要把abc所属路径的/mnt/sdcard去掉 **/
return new TarArchiveEntry(file, file.getPath().replace(mCompressSrcParentPath, ""));
}
private static void compressFile(File file, TarArchiveOutputStream taos) {
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
taos.putArchiveEntry(createTarArchiveEntry(file));
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
taos.write(data, 0, count);
}
bis.close();
taos.closeArchiveEntry();
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
/** usage: extract("/mnt/sdcard/Download.tar", "/mnt/sdcard") **/
public static void extract(String srcPath, String destPath) {
File srcFile = new File(srcPath);
if (srcFile.isFile()) {
extract(srcFile, destPath);
}
}
public static void extract(File srcFile, String destPath) {
extract(srcFile, new File(destPath));
}
public static void extract(File srcFile, File destFile) {
try {
TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(srcFile));
extract(destFile, tais);
tais.close();
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
private static void extract(File destFile, TarArchiveInputStream tais) {
TarArchiveEntry entry = null;
try {
while ((entry = tais.getNextTarEntry()) != null) {
String dirPath = destFile.getPath() + File.separator + entry.getName();
File dirFile = new File(dirPath);
createParentDirRecursively(dirFile);
if (entry.isDirectory()) {
dirFile.mkdirs();
} else {
extractFile(dirFile, tais);
}
}
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
private static void extractFile(File destFile, TarArchiveInputStream tais) {
BufferedOutputStream bos;
try {
bos = new BufferedOutputStream(new FileOutputStream(destFile));
int count;
byte data[] = new byte[BUFFER];
while ((count = tais.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
bos.close();
} catch (FileNotFoundException e) {
Log.e(TAG, e.toString());
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
private static void createParentDirRecursively(File dirFile) {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
createParentDirRecursively(parentFile);
parentFile.mkdir();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
apache commons-compress 之tar压缩文件夹的示例.zip (2个子文件)
commons-compress-1.4.1.jar 483KB
TarUtil.java 6KB
共 2 条
- 1
资源评论
- -烦-2017-05-22测试了一下,可以,但第一级目录有斜线,稍微改一下字符替换那部分就行
tedzyc
- 粉丝: 9
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 卡方数据.sav
- Python实现基于Django框架的二手物品购物网站设计源码+数据库.zip
- 基于Python和Django的二手物品购物网站设计源码+数据库脚本.zip
- 【计算机毕业设计】基于h5的移动网赚项目小程序【源码+lw+部署文档+讲解】
- 【计算机毕业设计】基于ssm-vue的oa系统设计与实现【源码+lw+部署文档+讲解】
- 基于pytorch实现的YOLOV5+SORT的车辆行人目标识别及追踪系统源码.zip
- 【计算机毕业设计】基于servlet+jdbc的在线选房系统设计与实现【源码+lw+部署文档+讲解】
- 投身科技创新,勇担时代先锋.pptx
- 【计算机毕业设计】基于SSM的仿微博系统的设计与实现【源码+lw+部署文档+讲解】
- 使用 Web Components 实现,遵循 Material You 设计规范的 Web 前端组件库
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功