package com.yabsz.decompCompr;
import java.io.File;
import java.util.ArrayList;
import java.util.List; public class main {
public static void main(String[] args) {
//需要下载commons-net-ftp-2.0.jar包下载地址:http://download.csdn.net/detail/u010696272/8006739 //第一步:获取一个文件夹下的所有的文件
List<File> files = file.getFiles("D:\\test");
List<File> sources = new ArrayList<File>();
for(File f : files){
System.out.println(f.getName());
System.out.println(f.toURI());
sources.add(f);
}
//第二步:把获取的文件下的文件压缩成一个tar文件 ,sources:要压缩的文件,target压缩的路径
File target = new File("D:\\test2\\release_package.tar");
file.compress(GZIPUtil.pack(sources, target)); try {
//第三步把tar文件压缩成tar.gz文件也就是gzip文件,siuress:要压缩的tar文件,gzget:压缩后的gz文件
String siuress ="D:/test2/release_package.tar";
String gzget = "D:/test2/release_package.tar.gz";
GZipUtils.compress(siuress, gzget);
//第四步解压:把gz文件解压成tar文件,sougz:要解压的gz,tar:解压后的tar文件
String sougz= "D:/test2/release_package.tar.gz";
String tar ="D:/test2/123.tar";
GZipUtils.decompress(sougz, tar);
//第五步,解压tar文件,soufile:要解压的tar路径,srcfile:后放的路径
String soufile = "D:\\test2\\release_package.tar";
String srcfile = "D:\\test2\\text";
TarUtils.dearchive(soufile,srcfile);
} catch (Exception e) {
e.printStackTrace();
} }
}
package com.yabsz.decompCompr;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream; public class file {
/**
*
* @Title: compress
* @Description: 将文件用tar压缩
* @param source
* 需要压缩的文件
* @return File 返回压缩后的文件
* @throws
*/
public static File compress(File source) {
File target = new File(source.getName() + ".gz");
FileInputStream in = null;
GZIPOutputStream out = null;
try {
in = new FileInputStream(source);
out = new GZIPOutputStream(new FileOutputStream(target));
byte[] array = new byte[1024];
int number = -1;
while ((number = in.read(array, 0, array.length)) != -1) {
out.write(array, 0, number);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
} if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
return target;
} /***
* 指定文件夹下的所有文件
* @param path
* @return
*/
public static List<File> getFiles(String path){
File root = new File(path);
List<File> files = new ArrayList<File>();
if(!root.isDirectory()){
files.add(root);
}else{
File[] subFiles = root.listFiles();
for(File f : subFiles){
files.addAll(getFiles(f.getAbsolutePath()));
}
}
return files;
}
}
package com.yabsz.decompCompr;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.GZIPOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils; /**
* @Title: GZIPUtil.java
* @Description: gzip文件压缩和解压缩工具类
* @author LM
* @date 2009-11-4 下午06:23:29
* @version V1.0
*/
public class GZIPUtil { /**
*
* @Title: pack
* @Description: 将一组文件打成tar包
* @param sources
* 要打包的原文件数组
* @param target
* 打包后的文件
* @return File 返回打包后的文件
* @throws
*/
public static File pack(List<File> sources, File target) {
FileOutputStream out = null;
try {
out = new FileOutputStream(target);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
TarArchiveOutputStream os = new TarArchiveOutputStream(out);
for (File file : sources) {
try {
System.out.println(file.getName());
os.putArchiveEntry(new TarArchiveEntry(file));
IOUtils.copy(new FileInputStream(file), os);
os.closeArchiveEntry(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} return target;
}
}
package com.yabsz.decompCompr;
/**
* 2010-4-13
*/ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; /**
* GZIP工具
*
* @author
* @since 1.0
*/
public abstract class GZipUtils { public static final int BUFFER = 1024;
public static final String EXT = ".gz"; /**
* 数据压缩
*
* @param data
* @return
* @throws Exception
*/
public static byte[] compress(byte[] data) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 压缩
compress(bais, baos); byte[] output = baos.toByteArray(); baos.flush();
baos.close(); bais.close(); return output;
} /**
* 文件压缩
*
* @param file
* @throws Exception
*/
public static void compress(File file) throws Exception {
compress(file, true);
} /**
* 文件压缩
*
* @param file
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void compress(File file, boolean delete) throws Exception {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getPath() + EXT); compress(fis, fos); fis.close();
fos.flush();
fos.close(); if (delete) {
file.delete();
}
} /**
* 数据压缩
*
* @param is
* @param os
* @throws Exception
*/
public static void compress(InputStream is, OutputStream os)
throws Exception { GZIPOutputStream gos = new GZIPOutputStream(os); int count;
byte data[] = new byte[BUFFER];
while ((count = is.read(data, 0, BUFFER)) != -1) {
gos.write(data, 0, count);
} gos.finish(); gos.flush();
gos.close();
} /**
* 文件压缩
*
* @param path
* @throws Exception
*/
public static void compress(String path) throws Exception {
compress(path, true);
} /**
* 文件压缩
*
* @param path
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void compress(String path, boolean delete) throws Exception {
File file = new File(path);
compress(file, delete);
} /**
* 文件压缩
*
* @param path
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void compress(String inputFileName, String outputFileName)
throws Exception {
FileInputStream inputFile = new FileInputStream(inputFileName);
FileOutputStream outputFile = new FileOutputStream(outputFileName);
compress(inputFile, outputFile);
inputFile.close();
outputFile.flush();
outputFile.close();
} /**
* 数据解压缩
*
* @param data
* @return
* @throws Exception
*/
public static byte[] decompress(byte[] data) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 解压缩 decompress(bais, baos); data = baos.toByteArray(); baos.flush();
baos.close(); bais.close(); return data;
} /**
* 文件解压缩
*
* @param file
* @throws Exception
*/
public static void decompress(File file) throws Exception {
decompress(file, true);
} /**
* 文件解压缩
*
* @param file
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void decompress(File file, boolean delete) throws Exception {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT,
""));
decompress(fis, fos);
fis.close();
fos.flush();
fos.close(); if (delete) {
file.delete();
}
} /**
* 文件解压缩
*
* @param file
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void decompress(String inputFileName, String outputFileName)
throws Exception {
FileInputStream inputFile = new FileInputStream(inputFileName);
FileOutputStream outputFile = new FileOutputStream(outputFileName);
decompress(inputFile, outputFile);
inputFile.close();
outputFile.flush();
outputFile.close();
} /**
* 数据解压缩
*
* @param is
* @param os
* @throws Exception
*/
public static void decompress(InputStream is, OutputStream os)
throws Exception { GZIPInputStream gis = new GZIPInputStream(is); int count;
byte data[] = new byte[BUFFER];
while ((count = gis.read(data, 0, BUFFER)) != -1) {
os.write(data, 0, count);
} gis.close();
} /**
* 文件解压缩
*
* @param path
* @throws Exception
*/
public static void decompress(String path) throws Exception {
decompress(path, true);
} /**
* 文件解压缩
*
* @param path
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void decompress(String path, boolean delete) throws Exception {
File file = new File(path);
decompress(file, delete);
} }
package com.yabsz.decompCompr;
/**
* 2010-4-20
*/ import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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工具
*
* @author
* @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 = 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();
} } }

java 压缩以及解压文件,有tar,zip,gz(gizp)和解压的更多相关文章

  1. java 压缩与解压

    最近复习到IO,想找个案例做一做,恰好下载了许多图片压缩包,查看图片很不方便,所以打算用IO把图片都解压到同一个文件夹下.然后集中打包. 本例使用jdk自带的ZipInputStream和ZipOut ...

  2. 压缩和解压文件:tar gzip bzip2 compress(转)

    tar[必要参数][选择参数][文件] 压缩:tar -czvf filename.tar.gz targetfile解压:tar -zxvf filename.tar.gz参数说明: -c 建立新的 ...

  3. java压缩与解压

    一 概述 1.目录进入点 目录进入点是文件在压缩文件中的映射,代表压缩文件.压缩文件时,创建目录进入点,将文件写入该目录进入点.解压时,获取目录进入点,将该目录进入点的内容写入硬盘指定文件. 如果目录 ...

  4. java压缩与解压文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  5. 文件解压缩 tar zip

    zip -e var-log-protected.zip /var/log/* Enter password: Verify password: updating: var/log/acpid (de ...

  6. java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)

    // java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...

  7. linux下tar gz bz2 tgz z等众多压缩文件的压缩与解压方法

    Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...

  8. linux下文件压缩与解压操作

    对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rap.可 ...

  9. Linux命令(16)压缩,解压文件

    tar: 简介:tar命令只是把目录打包成一个归档(文件),并不负责压缩.在tar命令中可以带参数调用gzip或bzip2压缩.因为gzip和bzip2只能压缩单个文件. 在linux下是不需要后缀名 ...

随机推荐

  1. 【例题 6-2 UVA - 514】Rails

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 栈模拟一下就好. 每个输出段后面都有一个空行. 包括最后一个. [代码] #include <bits/stdc++.h> ...

  2. 洛谷 P3871 中位数

    ->题目链接 题解: 暴力 经鉴定,此题数据水到没朋友. #include<algorithm> #include<iostream> #include<cstdi ...

  3. 【例题5-3 UVA - 10815】Andy's First Dictionary

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用stringstream来处理中间的标点. ->直接把他变成一个空格. 然后重新输入进去. set默认的字典序就是升序的了. ...

  4. 【record】11.14..11.20

    balabala

  5. Android使用BitmapFactory.Options解决加载大图片内存溢出问题

    由于Android对图片使用内存有限制,若是加载几兆的大图片便内存溢出.Bitmap会将图片的所有像素(即长x宽)加载到内存中,如果图片分辨率过大,会直接导致内存溢出(java.lang.OutOfM ...

  6. 使用ionic3快速开发webapp(二)

    本文整理了使用ionic3开发时会用到的一些最基本组件及用法 1.ion-tabs 最常见的通过标签切换页面: tabs.html <ion-tabs> <ion-tab [root ...

  7. (九)RabbitMQ消息队列-通过Headers模式分发消息

    原文:(九)RabbitMQ消息队列-通过Headers模式分发消息 Headers类型的exchange使用的比较少,以至于官方文档貌似都没提到,它是忽略routingKey的一种路由方式.是使用H ...

  8. 使用Opencv中matchTemplate模板匹配方法跟踪移动目标

    模板匹配是一种在图像中定位目标的方法,通过把输入图像在实际图像上逐像素点滑动,计算特征相似性,以此来判断当前滑块图像所在位置是目标图像的概率. 在Opencv中,模板匹配定义了6种相似性对比方式: C ...

  9. C#+HtmlAgilityPack

    C#+HtmlAgilityPack—糗事百科桌面版V2.0   最近在浏览以前自己上传的源码,发现在糗事百科桌面端源码评论区中,有人说现在程序不能用了.查看了一下源码运行情况,发现是正则表达式解析问 ...

  10. USACO--2.1The Castle

    思路:这个题目难在建图,開始的时候我想把每一个房间没有墙的面求出来,然后再和他邻近的房间加上一条边进行建图,后面发现要通过题目给定的条件求出房间那个面没有墙是十分困难的:后面參考了别人的思路,我们记录 ...