我们经常会使用WinZIP等压缩软件将文件进行压缩以方便传输。在java里面也提供了将文件进行压缩以减少传输时的数据量的类,可以很方便的将文件压缩成ZIP、JAR、GZIP等形式,GZIP主要是在Linux系统下的压缩文件。

下面主要讲的就是ZIP形式的压缩文件,而JAR、GZIP形式的压缩文件也是类似的用法。

ZIP是一种很常见的压缩形式,在java中要实现ZIP的压缩主要用到的是java.util.zip这个包里面的类。主要有ZipFile、 ZipOutputStream、ZipInputStream和ZipEntry。ZipOutputStream是用来压缩文件 的,ZipInputStream和ZipFile是用来解压缩文件的,在压缩和解压缩的过程中,ZipEntry都会用到。在java的Zip压缩文件 中,每一个子文件都是一个ZipEntry对象。


压缩文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipOutputStreamTest {
    public static void main(String args[]) throws IOException {
        test1();
        test2();
    }
                                    
    public static void test1() throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D:\\testZip.zip"), Charset.forName("GBK"));
        //实例化一个名称为ab.txt的ZipEntry对象
        ZipEntry entry = new ZipEntry("ab.txt");
        //设置注释
        zos.setComment("zip测试for单个文件");
        //把生成的ZipEntry对象加入到压缩文件中,而之后往压缩文件中写入的内容都会放在这个ZipEntry对象里面
        zos.putNextEntry(entry);
        InputStream is = new FileInputStream("D:\\ab.txt");
        int len = 0;
        while ((len = is.read()) != -1)
            zos.write(len);
        is.close();
        zos.close();
    }
                                    
    public static void test2() throws IOException {
        File inFile = new File("D:\\test");
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D:\\test.zip"), Charset.forName("GBK"));
        zos.setComment("多文件处理");
        zipFile(inFile, zos, "");
        zos.close();
    }
                                    
    public static void zipFile(File inFile, ZipOutputStream zos, String dir) throws IOException {
        if (inFile.isDirectory()) {
            File[] files = inFile.listFiles();
            for (File file:files)
                zipFile(file, zos, dir + "\\" + inFile.getName());
        } else {
            String entryName = null;
            if (!"".equals(dir))
                entryName = dir + "\\" + inFile.getName();
            else
                entryName = inFile.getName();
            ZipEntry entry = new ZipEntry(entryName);
            zos.putNextEntry(entry);
            InputStream is = new FileInputStream(inFile);
            int len = 0;
            while ((len = is.read()) != -1)
                zos.write(len);
            is.close();
        }
    }
                                    
}


解压缩文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class ZipInputStreamTest {
    public static void main(String args[]) throws IOException {
        File file = new File("D:\\test.zip");//压缩文件
        ZipFile zipFile = new ZipFile(file);//实例化ZipFile,每一个zip压缩文件都可以表示为一个ZipFile
        //实例化一个Zip压缩文件的ZipInputStream对象,可以利用该类的getNextEntry()方法依次拿到每一个ZipEntry对象
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file), Charset.forName("GBK"));
        ZipEntry zipEntry = null;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            String fileName = zipEntry.getName();
            File temp = new File("D:\\unpackTest\\" + fileName);
            if (! temp.getParentFile().exists())
                temp.getParentFile().mkdirs();
            OutputStream os = new FileOutputStream(temp);
            //通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
            InputStream is = zipFile.getInputStream(zipEntry);
            int len = 0;
            while ((len = is.read()) != -1)
                os.write(len);
            os.close();
            is.close();
        }
        zipInputStream.close();
    }
              
}

下面的是apache的解压缩包

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
/**
 * 可以处理中文文件名
 */
public class UnZip2
{
    private static final int buffer = 2048;
          
    public static void main(String[] args)
    {
        unZip("D:\\ss\\test.zip");
    }
          
    public static void unZip(String path)
    {
        int count = -1;
        int index = -1;
        String savepath = "";
        boolean flag = false;
              
        File file = null;
        InputStream is = null; 
        FileOutputStream fos = null; 
        BufferedOutputStream bos = null;
              
        savepath = path.substring(0, path.lastIndexOf("\\")) + "\\";
        try
        {
            ZipFile zipFile = new ZipFile(path);
            Enumeration<?> entries = zipFile.getEntries();
                  
            while(entries.hasMoreElements())
            {
                byte buf[] = new byte[buffer];
                      
                ZipEntry entry = (ZipEntry)entries.nextElement();
                      
                String filename = entry.getName();
                index = filename.lastIndexOf("/");
                if(index > -1)
                    filename = filename.substring(index+1);
                      
                filename = savepath + filename;
                      
                flag = isPics(filename);
                if(!flag)
                    continue;
                      
                file = new File(filename);
                file.createNewFile();
                      
                is = zipFile.getInputStream(entry);
                      
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos, buffer);
                      
                while((count = is.read(buf)) > -1)
                {
                    bos.write(buf, 0, count );
                }
                      
                fos.close();
                is.close();
            }
                  
            zipFile.close();
                  
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
    }
    public static boolean isPics(String filename)
    {
        boolean flag = false;
              
        if(filename.endsWith(".jpg") || filename.endsWith(".gif")  || filename.endsWith(".bmp") || filename.endsWith(".png"))
            flag = true;
              
        return flag;
    }
}