Java----文件的压缩与解压

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.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class FileZip
{

    public static void main(String[] args)
    {
        /*getZipFileName();*/
        /**压缩单个文件*/
        /*String filepath = "F:/iBoxClearDB_Orcl.sql";
        String zippath = "F:/test/db.zip";
        ZipFile(filepath, zippath);*/
        /**一次性压缩多个文件,文件存放至一个文件夹中*/
        /*String filepath = "E:/工作周报/";
        String zippath = "F:/test/week-job.zip";
        ZipMultiFile(filepath, zippath);*/
        /** 解压缩(解压缩单个文件)*/
        /*String zippath = "F:/test/db.zip";
        String outfilepath = "F:/test.sql";
        String filename = "iBoxClearDB_Orcl.sql";
        zipContraFile(zippath, outfilepath, filename);*/
        /**解压缩(压缩文件中包含多个文件)*/
        String zippath = "F:/test/week-job.zip";
        String outfilepath = "F:/";
        ZipContraMultiFile(zippath, outfilepath);
    }

    /**得到zip文件的文件名*/
    private static void getZipFileName()
    {
        File file = new File("F:/test/temp.zip");
        ZipFile zipFile = null;
        try
        {
            zipFile = new ZipFile(file);
            System.out.println("zip file name is :" + zipFile.getName());
        }
        catch (ZipException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (zipFile != null)
            {
                try
                {
                    zipFile.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    /** 压缩单个文件*/
    public static void zipFile(String filepath, String zippath)
    {
        InputStream input = null;
        ZipOutputStream zipOut = null;
        try
        {
            File file = new File(filepath);
            File zipFile = new File(zippath);
            input = new FileInputStream(file);
            zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
            zipOut.putNextEntry(new ZipEntry(file.getName()));
            int temp = 0;
            while ((temp = input.read()) != -1)
            {
                zipOut.write(temp);
            }
            System.out.println("zip " + filepath + " to " + zippath);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                input.close();
                zipOut.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    /** 一次性压缩多个文件,文件存放至一个文件夹中*/
    public static void zipMultiFile(String filepath, String zippath)
    {
        try
        {
            File file = new File(filepath);// 要被压缩的文件夹
            File zipFile = new File(zippath);
            InputStream input = null;
            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
            if (file.isDirectory())
            {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; ++i)
                {
                    input = new FileInputStream(files[i]);
                    zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
                    int temp = 0;
                    while ((temp = input.read()) != -1)
                    {
                        zipOut.write(temp);
                    }
                    input.close();
                }
            }
            else
            {//否则,则调用压缩单个文件的方法
                zipFile(filepath, zippath);
            }
            zipOut.close();
            System.out.println("zip directory is success");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /** 解压缩(解压缩单个文件)*/
    public static void zipContraFile(String zippath, String outfilepath, String filename)
    {
        try
        {
            File file = new File(zippath);//压缩文件路径和文件名
            File outFile = new File(outfilepath);//解压后路径和文件名
            ZipFile zipFile = new ZipFile(file);
            ZipEntry entry = zipFile.getEntry(filename);//所解压的文件名
            InputStream input = zipFile.getInputStream(entry);
            OutputStream output = new FileOutputStream(outFile);
            int temp = 0;
            while ((temp = input.read()) != -1)
            {
                output.write(temp);
            }
            input.close();
            output.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**解压缩(压缩文件中包含多个文件)可代替上面的方法使用。
    * ZipInputStream类
    * 当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,
    * 如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
    * */
    public static void ZipContraMultiFile(String zippath, String outzippath)
    {
        ZipInputStream zipInput = null;
        ZipFile zipFile = null;
        InputStream input = null;
        OutputStream output = null;
        try
        {
            File file = new File(zippath);
            File outFile = null;
            zipFile = new ZipFile(file);
            zipInput = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry = null;
            while ((entry = zipInput.getNextEntry()) != null)
            {
                System.out.println("解压缩" + entry.getName() + "文件");
                outFile = new File(outzippath + File.separator + entry.getName());
                if (!outFile.getParentFile().exists())
                {
                    outFile.getParentFile().mkdir();
                }
                if (!outFile.exists())
                {
                    outFile.createNewFile();
                }
                input = zipFile.getInputStream(entry);
                output = new FileOutputStream(outFile);
                int temp = 0;
                while ((temp = input.read()) != -1)
                {
                    output.write(temp);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                input.close();
                output.close();
                zipInput.close();
                zipFile.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值