java是否可以将多个文件压缩至gzip格式?如何做?
需求:在unix系统上实现压缩和解压缩(可能会是目录的压缩).
本人对于unix系统不熟悉,zip格式不知道在unxi可否???一般在unix系统上都用什么格式来进行压缩和解压缩呢?是否支持目录?
------解决方案--------------------ZipOutputStream
------解决方案--------------------import java.util.*;
import java.util.zip.*;
import java.io.*;
import javax.swing.*;
public class Tests
{
public static void main(String args[])
{
System.out.println( "请输入要保存的压缩文件名,例:test.zip: ");
try
{
BufferedReader buffin = new BufferedReader(new InputStreamReader(System.in));
String fileSource = buffin.readLine();
while(fileSource == " " || fileSource.length() == 0)
{
System.out.println( "输入错误,请重新输入: ");
fileSource = buffin.readLine();
}
File files = new File(fileSource);
FileOutputStream fout = new FileOutputStream(files);
CheckedOutputStream cout = new CheckedOutputStream(fout,new Adler32());
BufferedInputStream bin = null;
ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(cout));
ZipEntry ze;
System.out.println( "输入要压缩的文件列表,以空格;逗号分隔: ");
String fileAdd = buffin.readLine();
while(fileAdd == " " || fileAdd.length() == 0)
{
System.out.println( "输入错误,请重新输入: ");
fileAdd = buffin.readLine();
}
StringTokenizer fenxi = new StringTokenizer(fileAdd, " , ");
while(fenxi.hasMoreTokens())
{
String fs = fenxi.nextToken();
zout.putNextEntry(new ZipEntry(fs));
int c;
bin = new BufferedInputStream(new FileInputStream(fs));
while((c = bin.read()) != -1)
zout.write(c);
bin.close();
}
zout.close();
buffin.close();
System.out.println( "压缩成功! ");
}catch(Exception e){System.out.println( "错误! ");}
}
}
编译运行没问题了,只是只能处理当前目录下的文件,
加个界面,处理就方便了,参考下