使用java解压GZip文件

来源:互联网 发布:京东数据罗盘供应商 编辑:程序博客网 时间:2024/05/19 14:38

Java中有可以直接解压gzip文件的输入流。

/**     * 获取文件名(去掉.gz后缀)     * @param path     * @return     */    public static String getPrefix(String path) {        int index = path.lastIndexOf('.');        return path.substring(0, index);    }    public static void unGzip(String srcPath) {        unGzip(new File(srcPath));    }    /**     * 解压Gzip     * @param src 压缩文件     */    public static void unGzip(File src) {        String path = getPrefix(src.getAbsolutePath());        GZIPInputStream gzs = null;        BufferedOutputStream bos = null;        try {            gzs = new GZIPInputStream(new FileInputStream(src));            bos = new BufferedOutputStream(new FileOutputStream(path));            byte[] buf = new byte[102400];            int len = -1;            while ((len = gzs.read(buf)) != -1) {                bos.write(buf, 0, len);            }            bos.flush();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            FileUtil.close(gzs, bos);        }    }    /**     * 关闭流     * @param io     */    public static void close(Closeable ...io){        for (Closeable temp : io) {            try {                if(temp != null){                    temp.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }    }
原创粉丝点击