try {
// Open the compressed file
String inFilename = "infile.gzip";
GZIPInputStream in = new
GZIPInputStream(new FileInputStream
(inFilename));
// Open the output file
String outFilename = "outfile";
OutputStream out = new FileOutputStream
(outFilename);
// Transfer bytes from the compressed
file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Close the file and stream
in.close();
out.close();
} catch (IOException e) {
}
///////////////////////////
package hwq;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.zip.GZIPInputStream;
public class MultiMemberGZIPInputStream extends
GZIPInputStream {
public MultiMemberGZIPInputStream
(InputStream in, int size)
throws IOException
{
super(new PushbackInputStream
(in, size), size);
this.size = size;
}
public MultiMemberGZIPInputStream
(InputStream in) throws IOException
{
super(new PushbackInputStream
(in, 1024));
this.size = -1;
}
private MultiMemberGZIPInputStream
(MultiMemberGZIPInputStream parent)
throws IOException
{
super(parent.in);
this.size = -1;
this.parent = parent.parent ==
null ? parent : parent.parent;
this.parent.child = this;
}
private MultiMemberGZIPInputStream
(MultiMemberGZIPInputStream parent,
int size) throws
IOException
{
super(parent.in, size);
this.size = size;
this.parent = parent.parent ==
null ? parent : parent.parent;
this.parent.child = this;
}
private MultiMemberGZIPInputStream
parent;
private MultiMemberGZIPInputStream
child;
private int size;
private boolean eos;
public int read(byte[] inputBuffer, int
inputBufferOffset,
int inputBufferLen)
throws IOException
{
if (eos)
{
return -1;
}
if (this.child != null)
return this.child.read
(inputBuffer, inputBufferOffset,
inputBufferLen);
int charsRead = super.read
(inputBuffer, inputBufferOffset,
inputBufferLen);
if (charsRead == -1) {
int n =
inf.getRemaining() - 8;
if (n > 0) {
((PushbackInputStream) this.in).unread(buf, len
- n, n);
}
else
{
byte[] b = new
byte[1];
int ret =
in.read(b, 0, 1);
if (ret == -1)
{
eos =
true;
return
-1;
} else
((PushbackInputStream) this.in).unread(b, 0, 1);
}
MultiMemberGZIPInputStream child;
if (this.size == -1)
child = new
MultiMemberGZIPInputStream(this);
else
child = new
MultiMemberGZIPInputStream(this, this.size);
return child.read
(inputBuffer, inputBufferOffset,
inputBufferLen);
} else
return charsRead;
}
public static void main(String[] args)
{
try {
int nnumber;
FileInputStream fin = new
FileInputStream("d://hwq");
MultiMemberGZIPInputStream
MmGz = new MultiMemberGZIPInputStream(fin);
FileOutputStream fout = new
FileOutputStream("d:/hwq");
byte[] buf = new byte[1024];
nnumber = MmGz.read(buf, 0,
buf.length);
while (nnumber != -1) {
fout.write(buf, 0,
nnumber);
nnumber = MmGz.read
(buf, 0, buf.length);
}
MmGz.close();
fout.close();
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
///////////////////////
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class Test {
public static void main(String[] args) {
readFileAndWriteGZip();
unZipFile();
}
public static void readFileAndWriteGZip() {
try {
//打开需压缩文件作为文件输入流
FileInputStream fin = new
FileInputStream("d:/test.txt");
//建立压缩文件输出流
FileOutputStream fout = new
FileOutputStream("d:/test2.gzip");
//建立gzip压缩输出流
GZIPOutputStream gzout = new
GZIPOutputStream(fout);
//设定读入缓冲区尺寸
byte[] buf = new byte[1024];
int num;
while ((num = fin.read(buf)) != -1)
{
gzout.write(buf,0,num);
}
gzout.close();
fout.close();
fin.close();
} catch (Exception ex) {
System.err.println(ex.toString
());
}
}
public static void unZipFile() {
try {
//建立grip压缩文件输入流
FileInputStream fin = new
FileInputStream("d:/test2.gzip");
//建立gzip解压工作流
GZIPInputStream gzin = new
GZIPInputStream(fin);
//建立解压文件输出流
FileOutputStream fout = new
FileOutputStream("d:/test2.txt");
byte[] buf=new byte[1024];
int num;
while ((num = gzin.read
(buf,0,buf.length)) != -1)
{
fout.write(buf,0,num);
}
gzin.close();
fout.close();
fin.close();
} catch (Exception ex){
System.err.println(ex.toString());
}
}
}
////////////////////////
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 压缩实现类 <br>
* 主要实现: <br>
* <p>压缩单个文件、
* <p>压缩文件夹下的所有文件及子文件夹
*
*/
public class ZipTool {
/**
* 压缩单个文件
* @param filePath 文件路径
* @param fileName 文件名字
* @param objDir 压缩文件目标文件夹
* @param ojbZipName 压缩文件名字
* @return
* @throws IOException
*/
public boolean zip(String filePath, String
fileName, String objDir,
String ojbZipName) throws
IOException {
boolean tag = false;
ZipOutputStream zos = new
ZipOutputStream(new FileOutputStream(objDir
+ ojbZipName));
FileInputStream fis = new
FileInputStream(filePath + fileName);
byte[] b = new byte[4096];
int i = 0;
zos.putNextEntry(new ZipEntry
(fileName));
while ((i = (fis.read(b))) > 0) {
zos.write(b, 0, i);
}
fis.close();
zos.close();
return tag;
}
/**
* 压缩一个文件夹下的所有文件 注意中间路径连接
用"/" 如:c:/tt/ttt
* @param srcPath 要压缩的文件夹路径
* @param objZipPath 目标文件夹路径
* @param ojbZipName 目标压缩文件名字
* @return
*/
public boolean zipDir(String srcPath, String
objZipPath, String ojbZipName){
ZipOutputStream zos = null;
try {
File objFile = new File(objZipPath ,
ojbZipName);
zos = new ZipOutputStream(new
FileOutputStream(objFile));
if (srcPath == null) {
System.out.println("传入的源文件
夹路径字符串不能为空!");
throw new
java.lang.NullPointerException();
}
String dirName = "";
File file = new File(srcPath);
if (!file.isDirectory()) {
throw new Exception("传入了不正确
的源文件夹路径!");
} else {
dirName = srcPath.substring
(srcPath.lastIndexOf("/") + 1);
if (dirName == null ||
"".equals(dirName)) {
String subStr =
srcPath.substring(0, srcPath.length() - 2);
dirName = subStr.substring
(subStr.lastIndexOf("/") + 1);
}
ZipEntry ze = new ZipEntry
(dirName + "/");
zos.putNextEntry(ze);
if (dirName == null ||
"".equals(dirName)) {
throw new Exception("传入了不
正确的源文件夹路径!");
}
}
File[] files = file.listFiles();
for (int i = 0; i < files.length; i
++) {
zipFile(dirName + "/", files[i],
zos);
}
return true;
} catch (Exception e) {
System.out.println("压缩文件时出现异
常!");
e.printStackTrace();
return false;
}finally{
if(zos != null){
try {
zos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/**
* 用来压缩文件夹下的所有子文件
* 此方法为一个递归调法方法
* @param zipPath 要压缩的文件在压缩包中的相对
路径
* @param file 要压缩的文件引用
* @param zos 压缩文件输出流
* @throws IOException
*/
private void zipFile(String zipPath, File
file, ZipOutputStream zos)
throws IOException {
//是文件夹的操作
if (file.isDirectory()) {
ZipEntry ze = new ZipEntry(zipPath +
file.getName() + "/");
zos.putNextEntry(ze);
//递归调用
for (int i = 0; i < file.listFiles
().length; i++) {
zipFile(zipPath + file.getName()
+ "/", file.listFiles()[i], zos);
}
//是文件时的操作
}else{
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
ZipEntry ze = new ZipEntry
(zipPath + file.getName());
zos.putNextEntry(ze);
byte []b = new byte[4096];
int i = 0;
while( ( i = (fis.read(b)) ) >
0 ){
zos.write(b, 0, i);
}
}finally{
if(fis != null){
fis.close();
}
}
}
}
public static void main(String[] args) {
ZipTool zt = new ZipTool();
try {
zt.zipDir("c:/gif", "c:/",
"test.zip");
} catch (Exception e) {
e.printStackTrace();
}
}
}
////////////////////////
java解压xx.tar.gz格式的压缩包(转)
java技术 2010-08-16 16:11:32 阅读76 评论0 字号
:大中小 订阅
先引入commons-compress-1.0-SNAPSHOT.jar
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 java.util.zip.GZIPInputStream;
import
org.apache.commons.compress.archivers.ArchiveExc
eption;
import
org.apache.commons.compress.archivers.ArchiveInp
utStream;
import
org.apache.commons.compress.archivers.ArchiveStr
eamFactory;
import
org.apache.commons.compress.archivers.tar.TarArc
hiveEntry;
/**
* 解压tar.gz文件包
*
*/
public class GZip {
private BufferedOutputStream
bufferedOutputStream;
String zipfileName = null;
public GZip(String fileName) {
this.zipfileName = fileName;
}
/*
* 执行入口,rarFileName为需要解压的文件路径(具
体到文件),destDir为解压目标路径
*/
public static void unTargzFile(String
rarFileName, String destDir) {
GZip gzip = new GZip(rarFileName);
String outputDirectory = destDir;
File file = new File(outputDirectory);
if (!file.exists()) {
file.mkdir();
}
gzip.unzipOarFile(outputDirectory);
}
public void unzipOarFile(String
outputDirectory) {
FileInputStream fis = null;
ArchiveInputStream in = null;
BufferedInputStream bufferedInputStream =
null;
try {
fis = new FileInputStream
(zipfileName);
GZIPInputStream is = new
GZIPInputStream(new BufferedInputStream(
fis));
in = new ArchiveStreamFactory
().createArchiveInputStream("tar", is);
bufferedInputStream = new
BufferedInputStream(in);
TarArchiveEntry entry =
(TarArchiveEntry) in.getNextEntry();
while (entry != null) {
String name = entry.getName();
String[] names = name.split("/");
String fileName = outputDirectory;
for(int i = 0;i<names.length;i++){
String str = names[i];
fileName = fileName +
File.separator + str;
}
if (name.endsWith("/")) {
mkFolder(fileName);
} else {
File file = mkFile(fileName);
bufferedOutputStream = new
BufferedOutputStream(
new FileOutputStream
(file));
int b;
while ((b =
bufferedInputStream.read()) != -1) {
bufferedOutputStream.write
(b);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
entry = (TarArchiveEntry)
in.getNextEntry();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ArchiveException e) {
e.printStackTrace();
} finally {
try {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void mkFolder(String fileName) {
File f = new File(fileName);
if (!f.exists()) {
f.mkdir();
}
}
private File mkFile(String fileName) {
File f = new File(fileName);
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return f;
}
}
////////////////
BufferedReader br = new BufferedReader(new
FileReader(new File("C:/aaa.txt")));
BufferedWriter bw = new BufferedWriter(new
FileWriter(new File("C:/bbb.txt")));
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
System.out.println("复制成功");