java压缩,解压文件
来源:互联网 发布:登录我的淘宝 编辑:程序博客网 时间:2024/05/25 23:59
@Test //压缩到父目录下 public void run() throws Exception{ File srcFile=new File("g:\\a\\2222.txt.txt"); //将2222.txt.txt 压缩成.tar存放到 a同级目录下 TarUtils.archive(srcFile); File srcFile2=new File("g:\\a\\"); //a下所有的内容 压缩,,放在a 同级目录下 TarUtils.archive(srcFile2); } @Test //压缩到指定目录下 public void run2() throws Exception{ //要压缩的文件 File srcFile=new File("g:\\a\\2222.txt.txt"); //压缩后的文件 存放在b文件夹中 。压缩文件: name.tar String destPath = "g:\\a\\b\\" + "name" + ".tar"; File desFile=new File(destPath); TarUtils.archive(srcFile,desFile); }
jar:commons-compress-1.4.1.jar
工具类:TarUtils
/** * TAR工具 * @since 1.0 */public abstract class TarUtils { private static final String BASE_DIR = ""; // 符号"/"用来作为目录标识判断符 private static final String PATH = "/"; private static final int BUFFER = 1024; private static final String EXT = ".tar"; /** * 归档 * * @param srcPath * @param destPath * @throws Exception */ public static void archive(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); archive(srcFile, destPath); } /** * 归档 * * @param srcFile * 源路径 * @param destPath * 目标路径 * @throws Exception */ public static void archive(File srcFile, File destFile) throws Exception { TarArchiveOutputStream taos = new TarArchiveOutputStream( new FileOutputStream(destFile)); archive(srcFile, taos, BASE_DIR); taos.flush(); taos.close(); } /** * 归档 * * @param srcFile * @throws Exception */ public static void archive(File srcFile) throws Exception { String name = srcFile.getName(); String basePath = srcFile.getParent(); String destPath = basePath + name + EXT; archive(srcFile, destPath); } /** * 归档文件 * * @param srcFile * @param destPath * @throws Exception */ public static void archive(File srcFile, String destPath) throws Exception { archive(srcFile, new File(destPath)); } /** * 归档 * * @param srcPath * @throws Exception */ public static void archive(String srcPath) throws Exception { File srcFile = new File(srcPath); archive(srcFile); } /** * 归档 * * @param srcFile * 源路径 * @param taos * TarArchiveOutputStream * @param basePath * 归档包内相对路径 * @throws Exception */ private static void archive(File srcFile, TarArchiveOutputStream taos, String basePath) throws Exception { if (srcFile.isDirectory()) { archiveDir(srcFile, taos, basePath); } else { archiveFile(srcFile, taos, basePath); } } /** * 目录归档 * * @param dir * @param taos * TarArchiveOutputStream * @param basePath * @throws Exception */ private static void archiveDir(File dir, TarArchiveOutputStream taos, String basePath) throws Exception { File[] files = dir.listFiles(); if (files.length < 1) { TarArchiveEntry entry = new TarArchiveEntry(basePath + dir.getName() + PATH); taos.putArchiveEntry(entry); taos.closeArchiveEntry(); } for (File file : files) { // 递归归档 archive(file, taos, basePath + dir.getName() + PATH); } } /** * 数据归档 * * @param data * 待归档数据 * @param path * 归档数据的当前路径 * @param name * 归档文件名 * @param taos * TarArchiveOutputStream * @throws Exception */ private static void archiveFile(File file, TarArchiveOutputStream taos, String dir) throws Exception { /** * 归档内文件名定义 * * <pre> * 如果有多级目录,那么这里就需要给出包含目录的文件名 * 如果用WinRAR打开归档包,中文名将显示为乱码 * </pre> */ TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName()); entry.setSize(file.length()); taos.putArchiveEntry(entry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream( 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(); } /** * 解归档 * * @param srcFile * @throws Exception */ public static void dearchive(File srcFile) throws Exception { String basePath = srcFile.getParent(); dearchive(srcFile, basePath); } /** * 解归档 * * @param srcFile * @param destFile * @throws Exception */ public static void dearchive(File srcFile, File destFile) throws Exception { TarArchiveInputStream tais = new TarArchiveInputStream( new FileInputStream(srcFile)); dearchive(destFile, tais); tais.close(); } /** * 解归档 * * @param srcFile * @param destPath * @throws Exception */ public static void dearchive(File srcFile, String destPath) throws Exception { dearchive(srcFile, new File(destPath)); } /** * 文件 解归档 * * @param destFile * 目标文件 * @param tais * ZipInputStream * @throws Exception */ private static void dearchive(File destFile, TarArchiveInputStream tais) throws Exception { TarArchiveEntry entry = null; while ((entry = tais.getNextTarEntry()) != null) { // 文件 String dir = destFile.getPath() + File.separator + entry.getName(); File dirFile = new File(dir); // 文件检查 fileProber(dirFile); if (entry.isDirectory()) { dirFile.mkdirs(); } else { dearchiveFile(dirFile, tais); } } } /** * 文件 解归档 * * @param srcPath * 源文件路径 * * @throws Exception */ public static void dearchive(String srcPath) throws Exception { File srcFile = new File(srcPath); dearchive(srcFile); } /** * 文件 解归档 * * @param srcPath * 源文件路径 * @param destPath * 目标文件路径 * @throws Exception */ public static void dearchive(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); dearchive(srcFile, destPath); } /** * 文件解归档 * * @param destFile * 目标文件 * @param tais * TarArchiveInputStream * @throws Exception */ private static void dearchiveFile(File destFile, TarArchiveInputStream tais) throws Exception { BufferedOutputStream 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(); } /** * 文件探针 * * <pre> * 当父目录不存在时,创建目录! * </pre> * * @param dirFile */ private static void fileProber(File dirFile) { File parentFile = dirFile.getParentFile(); if (!parentFile.exists()) { // 递归寻找上级目录 fileProber(parentFile); parentFile.mkdir(); } }}
阅读全文
0 0
- JAVA 压缩解压文件
- JAVA文件压缩、解压
- 压缩解压文件--Java
- java压缩,解压文件
- java 压缩、解压文件、文件夹。
- java压缩与解压文件
- Java文件解压与压缩
- Java压缩解压文件/文件夹
- JAVA压缩及解压文件
- java压缩和解压文件
- java文件压缩与解压
- java 解压,压缩单个文件
- java压缩解压ZIP文件
- 用java压缩/解压文件
- 用java压缩及解压文件 AntZip
- Java 解压和压缩密码zip文件
- java实现zip文件压缩,解压
- Java压缩与解压rar文件
- 没有标题
- 将十进制数转换为十六进制数
- 一个表单同时提交多条记录
- BACnet协议
- X轴转圈动画
- java压缩,解压文件
- 深度图像与点云数据之间的转换关系
- Qt程序自动重启的实现
- 回顾春运——灵宝西站派出所第一警务区: 张千
- 深入学习Android——AIDL机制
- java解析PDF文件,并获取到指定数据.Eg
- HBase原理-数据读取流程解析
- Webmagic爬虫--②爬自己的CSDN博客列表
- CentOS(Red Hat内核)MongoDB3.4企业版安装