java使用tar算法压缩解压缩文件、数据流、byte[]字节数组
全栈工程师开发手册 (作者:栾鹏)
java教程全解
java使用tar算法压缩解压缩文件、数据流、byte[]字节数组
需要添加org.apache.commons.compress包,下载
测试代码
public static void main(String[] args) {try {String inputStr="111111111111111111111111111111111";//压缩文件测试byte[] contentOfEntry = inputStr.getBytes(); FileOutputStream fos = new FileOutputStream("test.txt"); fos.write(contentOfEntry); fos.flush(); fos.close(); //压缩文件TarUtils.archive("test.txt"); //解压缩文件TarUtils.dearchive("test.txt" + ".tar"); File file = new File("test.txt"); FileInputStream fis = new FileInputStream(file); DataInputStream dis = new DataInputStream(fis); byte[] data = new byte[(int) file.length()]; dis.readFully(data); fis.close(); String outputStr = new String(data);System.out.println(outputStr);//测试压缩目录String path = "d:/srcdir"; TarUtils.archive(path); TarUtils.dearchive(path + ".tar", "d:/desdir"); } catch (Exception e) {e.printStackTrace();}}
tar压缩解压缩工具类的实现
package com.lp.app.io;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //TAR工具
public class TarUtils { private static final String BASE_DIR = ""; // 符号"/"用来作为目录标识判断符 private static final String PATH = "/"; //归档 public static void archive(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); archive(srcFile, destPath); } //归档 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(); } //归档 public static void archive(File srcFile) throws Exception { String name = srcFile.getName(); String basePath = srcFile.getParent(); String destPath = basePath +"\\"+ name + ".tar"; if (basePath==null) {destPath = name + ".tar";}archive(srcFile, destPath); } //归档文件 public static void archive(File srcFile, String destPath) throws Exception { archive(srcFile, new File(destPath)); } //归档 public static void archive(String srcPath) throws Exception { File srcFile = new File(srcPath); archive(srcFile); } //归档 private static void archive(File srcFile, TarArchiveOutputStream taos, String basePath) throws Exception { if (srcFile.isDirectory()) { archiveDir(srcFile, taos, basePath); } else { archiveFile(srcFile, taos, basePath); } } //目录归档 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); } } //数据归档 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[1024]; while ((count = bis.read(data, 0, 1024)) != -1) { taos.write(data, 0, count); } bis.close(); taos.closeArchiveEntry(); } //解归档public static void dearchive(File srcFile) throws Exception { String basePath = srcFile.getParent();if (basePath==null) {basePath="";}dearchive(srcFile, basePath); } //解归档 public static void dearchive(File srcFile, File destFile) throws Exception { TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(srcFile)); dearchive(destFile, tais); tais.close(); } //解归档 public static void dearchive(File srcFile, String destPath)throws Exception { dearchive(srcFile, new File(destPath)); } //文件 解归档 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); } } } //文件 解归档 public static void dearchive(String srcPath) throws Exception { File srcFile = new File(srcPath); dearchive(srcFile); } //文件 解归档 public static void dearchive(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); dearchive(srcFile, destPath); } //文件解归档 private static void dearchiveFile(File destFile, TarArchiveInputStream tais) throws Exception { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)); int count; byte data[] = new byte[1024]; while ((count = tais.read(data, 0, 1024)) != -1) { bos.write(data, 0, count); } bos.close(); } //文件探针 ,当父目录不存在时,创建目录! private static void fileProber(File dirFile) { File parentFile = dirFile.getParentFile(); if (!parentFile.exists()) { // 递归寻找上级目录 fileProber(parentFile); parentFile.mkdir(); } } }
java使用tar算法压缩解压缩文件、数据流、byte[]字节数组相关推荐
- java使用BZip算法压缩解压缩文件、数据流、byte[]字节数组
全栈工程师开发手册 (作者:栾鹏) java教程全解 java使用BZip算法压缩解压缩文件.数据流.byte[]字节数组 需要添加org.apache.commons.compress包,下载 测试 ...
- java实现zlib压缩解压缩:文件、byte[]字节数组,数据流
全栈工程师开发手册 (作者:栾鹏) java教程全解 java实现zlib压缩解压缩. 有关ZLib可参见官方主页 http://www.zlib.net/ ZLib可以简单的理解为压缩/解压缩算法, ...
- 【linux】tar指令压缩解压缩文件夹、文件命令详解
1. tar常用命令: 压缩当前目录下文件夹/文件test到test.tar.gz: tar -zcvf test.tar.gz test 解压缩当前目录下的file.tar.gz到file: tar ...
- java Hutool发送邮件附带附件 使用文件流、字节数组方式
MailUtil.send();无法发送文件流, //这个方式只能传入本地File,无法传入网络资源 MailUtil.send(); 内置Mail类实现方式 public static void m ...
- 将图片文件通过byte[]字节数组Base64加密后给前端显示
问题描述:把图片上传到服务器,拿到url地址后把url地址给前端,该url地址可以直接获取到图片文件的字节数组进行base64加密后的字符串(data) 解决方法: //将图片转化为字节数组 byte ...
- Java 原生实现 URL Decode 功能(从 Byte 字节数组转 Unicode 字符)
Java 实现 URL Decode 背景: ElasticSearch 自定义脚本 Painless 当中可以支持 Java 的部分 API,但 6.x 版本没有对 URLDecode 的支持(发现 ...
- Unity录音并保存成wav文件或者byte[]字节数组
需要去商店下载[Open WAV Parser]这个插件,目前是免费的! ps. 用这个插件之前,用Unity原生的Microphone折腾了快两天.最终觉得还是这个插件好用啊!推荐!
- java实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
全栈工程师开发手册 (作者:栾鹏) java教程全解 java实现gzip压缩解压缩byte[]字节数组,文件,字符串. 测试代码 public static void main(String[] a ...
- c#实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
全栈工程师开发手册 (作者:栾鹏) c#教程全解 c#实现gzip压缩解压缩byte[]字节数组,文件,字符串. 测试代码 static void Main() {//测试字符串String inpu ...
最新文章
- check $m2_home environment variable and mvn script match
- Java访问修饰符public,private,protected,以及不写(默认)时的区别?
- 数据仓库--主题和主题域
- EditPlus3.21注册码
- setname_Python线程类| setName()方法与示例
- Linux内存是怎么工作的
- 十四周二次课(3月27日)
- 研磨设计模式之工厂方法模式-2
- IT软件开发人员必去的10个社区
- Tungsten Fabric SDN — within AWS EKS
- python中function是什么意思_Python中的Function定义方法
- oracle+suspend+参数,oracle数据库的挂起(Suspending)和恢复(Resuming)
- asp.net 获取网页源文件的方法
- 云效,云原生时代一站式研发平台
- 关于Scanner一些用法
- 仿头条新闻资讯dz模板/Discuz新闻资讯商业版GBK模板
- 2013年7月美国Boston之旅
- 实战 | Kaggle竞赛:华盛顿特区首都自行车租赁预测
- 富力等待黎明:李思廉走在还债的漫漫长路
- vue拓扑图、组织结构图插件使用(orgChart.js)
热门文章
- 登量子位“AI领航企业榜单”,联想用实力说话
- 目前效果最好、应用较广且比较成熟的语音识别模型是什么?
- 第P题 扔盘子(双重循环就行) 详解=====有一口井,井的高度为N,每隔1个单位它的宽度有变化。现在从井口往下面扔圆盘,如果圆盘的宽度大于井在某个高度的宽度
- python怎么将txt转为excel_使用matlab或python将txt文件转为excel表格
- python如何输出整数逆序_python字符串类型及操作
- 【java笔记】java语言的跨平台性和运行环境
- 最简单的基于FFmpeg的移动端例子附件:SDL Android HelloWorld
- 如何让MySQL语句执行加速
- HDOJ水题集合8:DBFS
- 通用数据接口USB3.0,雷电3,TypeC之间