集册 Java实例教程 将压缩文件解压缩为普通文件

将压缩文件解压缩为普通文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

405
将压缩文件解压缩为普通文件


//package com.nowjava;
/* 
*来 自
 时 代      J a v a   公   众 号 - nowjava.com
*/

import java.io.Closeable;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;


import java.util.zip.GZIPInputStream;


public class Main {

    /**

     * ungzip a gzipped file to a normal file

     * @param gzippedFile

     * @param ungzippedFile

     * @throws IOException

     */

    public static void unzip(File gzippedFile, File ungzippedFile)

            throws IOException {

        GZIPInputStream gis = null;

        FileOutputStream fos = null;
        /** 
         来自 N o  w  J a v a . c o m - 时  代  Java**/

        try {

            gis = new GZIPInputStream(new FileInputStream(gzippedFile));

            fos = new FileOutputStream(ungzippedFile);

            byte[] buffer = new byte[1024];

            int len = -1;

            while ((len = gis.read(buffer)) != -1) {

                fos.write(buffer, 0, len);

            }

        } finally {

            close(gis);

            close(fos);

        }

    }


    private static 
展开阅读全文