Java IO
为了减少传输时的数据量 在Java中提供了专门的压缩流将文件或者文件夹压缩成zip,gzip,jar等文件形式。
压缩流实现
Java支持的三种压缩格式:zip、jar、gzip。
1.zip是一种较为常见的压缩格式,Java提供了java.util.zip包,常用类:
- ZipInputStream
- ZipOutputStream
- ZipEntry
- ZipFile
依靠上述4个类完成zip类型文件操作。
2.Jar是Java专有的一种压缩格式,Java提供了java.util.jar包,常用类:
- JarInputStream
- JarOutputStream
- JarEntry
- JarFile
3.GZIP是Unix系统的文件压缩格式,Linux系统中经常用的.gz 文件就是GZIP格式,Java提供了
- GZipInputStream
- GZipOutputStream
本文以zip 压缩/解压缩为例来讲解。
压缩
@Test
public void testZip() throws IOException {
File zipFile = new File("D:\\log.zip"); //压缩后的文件
ZipOutputStream zipOut = null;
try{
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
File dir = new File("D:\\logs\\info\\"); //待压缩的文件夹
File[] files = dir.listFiles();
for (File file : files){
InputStream in = null;
try{
zipOut.putNextEntry(new ZipEntry(file.getName())) ;
zipOut.setComment("test zip") ; // 设置注释
in = new FileInputStream(file) ; // 定义文件的输入流
IoUtils.copy(in, zipOut);
}finally {
IoUtils.closeQuietly(in);
}
}
}finally {
IoUtils.closeQuietly(zipOut);
}
System.out.println("压缩文件完成");
}
解压缩
@Test
public void testUnZip() throws IOException {
File dir = new File("D:\\test");
if(!dir.exists()){
dir.mkdirs();
}
File srcFile = new File("D:\\log.zip");
ZipFile zipFile = new ZipFile(srcFile);
ZipInputStream zipIn = null;
try{
zipIn = new ZipInputStream(new FileInputStream(srcFile));
ZipEntry entry = null;
while((entry = zipIn.getNextEntry())!=null){
System.out.println("解压缩文件:" + entry.getName());
OutputStream out = null;
InputStream in = null;
try{
File file = new File(dir, entry.getName());
in = zipFile.getInputStream(entry);
out = new FileOutputStream(file);
IoUtils.copy(in, out);
}finally {
IoUtils.closeQuietly(out);
IoUtils.closeQuietly(in);
}
}
}finally {
IoUtils.closeQuietly(zipIn);
}
System.out.println("解压缩文件完成");
}
IoUtils.java
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* ${DESCRIPTION}
*
* @author Ricky Fung
* @create 2016-10-31 10:19
*/
public class IoUtils {
public static void closeQuietly(InputStream input){
closeQuietly((Closeable)input);
}
public static void closeQuietly(OutputStream output){
closeQuietly((Closeable)output);
}
public static void closeQuietly(Closeable closeable) {
try {
if(closeable != null) {
closeable.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void closeQuietly(Closeable... closeables){
for (Closeable closeable : closeables){
closeQuietly(closeable);
}
}
public static long copy(InputStream in, OutputStream out, int bufferSize) throws IOException {
byte[] buff = new byte[bufferSize];
return copy(in, out, buff);
}
public static long copy(InputStream in, OutputStream out) throws IOException {
byte[] buff = new byte[1024];
return copy(in, out, buff);
}
public static long copy(InputStream in, OutputStream out, byte[] buff) throws IOException {
long count = 0;
int len = -1;
while((len=in.read(buff, 0, buff.length))!=-1){
out.write(buff, 0, len);
count += len;
}
return count;
}
}