JAVA中压缩与解压缩

以压缩Zip文件为例。主要是通过ZipOutputStream类实现。解压缩主要使用ZipFile类和ZipInputStream以及ZipEntry类。

package main;

import java.io.*;
import java.util.*;
import java.util.zip.*;
public class Main
{

    public static final Integer BUFFERSIZE = 1024*1024;
    public static void main(String[] args) throws Exception
    {
        File fileNeedToBeCompressed = new File("C:" + File.separator + "D" + File.separator + "code" + File.separator + "output.xml");
        StringBuffer zipFilePath = new StringBuffer(fileNeedToBeCompressed.getParent());
        zipFilePath.append(File.separator).append(getFileName(fileNeedToBeCompressed.getName())).append(".zip");
        File compressedFile = new File(zipFilePath.toString());
        
        
        InputStream is = null;
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(compressedFile));
        
        if(fileNeedToBeCompressed.isDirectory())
        {
            //压缩一个文件夹
            
            
            File[] files = fileNeedToBeCompressed.listFiles();
            for(File file : files)
            {
                compressFile(file, is, zos);
            }
        }
        else
        {
            //压缩一个文件
            
            compressFile(fileNeedToBeCompressed,is,zos);
        }
        zos.close();
        
        
        System.out.println("///~ Main done");
    }
    
    public static String getFileName(String fileName)
    {
        if(fileName.lastIndexOf('.') < 0)
        {
            return fileName;
        }
        else
        {
            return fileName.substring(0,fileName.lastIndexOf('.'));
        }
    }
    
    public static void compressFile(File fileNeedToBeCompressed,InputStream is,ZipOutputStream zos) throws Exception
    {
        System.out.println("正在压缩:" + fileNeedToBeCompressed.getName());
        is = new FileInputStream(fileNeedToBeCompressed);
        zos.putNextEntry(new ZipEntry(fileNeedToBeCompressed.getName()));
        int bytesReaded = 0;
        byte[] buffer = new byte[BUFFERSIZE];
        while((bytesReaded = is.read(buffer)) > 0)
        {
            zos.write(buffer,0,bytesReaded);
        }
        is.close();
    }

}

 

ZipInputStream 获取压缩文件中的每个ZipEntry,然后ZipFile通过ZipEntry拿到输入流。

package main;

import java.io.*;
import java.util.*;
import java.util.zip.*;
public class Main
{

    public static final Integer BUFFERSIZE = 1024*1024;
    public static void main(String[] args) throws Exception
    {
//        {
//            File fileNeedToBeCompressed = new File("C:" + File.separator + "D" + File.separator + "code" + File.separator + "resource");
//            StringBuffer zipFilePath = new StringBuffer(fileNeedToBeCompressed.getParent());
//            zipFilePath.append(File.separator).append(getFileName(fileNeedToBeCompressed.getName())).append(".zip");
//            File compressedFile = new File(zipFilePath.toString());
//            
//            
//            InputStream is = null;
//            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(compressedFile));
//            
//            if(fileNeedToBeCompressed.isDirectory())
//            {
//                //压缩一个文件夹
//                File[] files = fileNeedToBeCompressed.listFiles();
//                for(File file : files)
//                {
//                    compressFile(file, is, zos);
//                }
//            }
//            else
//            {
//                //压缩一个文件
//                
//                compressFile(fileNeedToBeCompressed,is,zos);
//            }
//            zos.close();
//        }
        
        {
            String zipFilePath = "C:" + File.separator + "D" + File.separator + "code" + File.separator + "resource.zip";
            ZipFile zipFile = new ZipFile(zipFilePath);
            ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(zipFilePath)));
            ZipEntry tempZipEntry = null;
            OutputStream os = null;
            byte[] buffer = new byte[BUFFERSIZE];
            File unzipedFolder = new File(getFileName(zipFile.getName()));
            int bytesReaded = 0;
            if(!unzipedFolder.exists())
                unzipedFolder.mkdirs();
            while((tempZipEntry = zis.getNextEntry())!= null)
            {
                System.out.println("正在解压:" + tempZipEntry.getName());
                File newFile = new File(unzipedFolder.getPath() + File.separator + tempZipEntry.getName());
                InputStream is = zipFile.getInputStream(tempZipEntry);
                os = new FileOutputStream(newFile);
                while(( bytesReaded = is.read(buffer)) > 0)
                {
                    os.write(buffer, 0, bytesReaded);
                }
                os.close();
            }
            zipFile.close();
            zis.close();
        }
        System.out.println("///~ Main done");
    }
    
    public static String getFileName(String fileName)
    {
        if(fileName.lastIndexOf('.') < 0)
        {
            return fileName;
        }
        else
        {
            return fileName.substring(0,fileName.lastIndexOf('.'));
        }
    }
    
    public static void compressFile(File fileNeedToBeCompressed,InputStream is,ZipOutputStream zos) throws Exception
    {
        System.out.println("正在压缩:" + fileNeedToBeCompressed.getName());
        is = new FileInputStream(fileNeedToBeCompressed);
        zos.putNextEntry(new ZipEntry(fileNeedToBeCompressed.getName()));
        int bytesReaded = 0;
        byte[] buffer = new byte[BUFFERSIZE];
        while((bytesReaded = is.read(buffer)) > 0)
        {
            zos.write(buffer,0,bytesReaded);
        }
        is.close();
    }

}

 

posted on 2016-06-18 09:19  kuillldan  阅读(402)  评论(0编辑  收藏  举报