zoukankan      html  css  js  c++  java
  • 图片解压缩

    import java.io.*;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;

    /**
    * User : Jixiaohu
    * Date : 2017-07-03.
    * Time : 16:18.
    */
    public class GzipUtils {

    public static byte[] gzip(byte[] data) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(bos);
    gzip.write(data);
    gzip.finish();
    gzip.close();
    byte[] ret = bos.toByteArray();
    bos.close();
    return ret;
    }

    public static byte[] ungzip(byte[] data) throws Exception {
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    GZIPInputStream gzip = new GZIPInputStream(bis);
    byte[] buf = new byte[1024];
    int num = -1;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while ((num = gzip.read(buf, 0, buf.length)) != -1) {
    bos.write(buf, 0, num);
    }
    gzip.close();
    bis.close();
    byte[] ret = bos.toByteArray();
    bos.flush();
    bos.close();
    return ret;
    }

    public static void main(String[] args) throws Exception {
    //读取文件
    String readPath = System.getProperty("user.dir") + File.separator
    + "sources" + File.separatorChar + "006.jpg";
    System.out.println(readPath);
    File file = new File(readPath);
    FileInputStream in = new FileInputStream(file);
    byte[] data = new byte[in.available()];
    in.read(data);
    in.close();

    System.out.println("文件原始大小:" + data.length);

    //测试压缩
    byte[] ret1 = GzipUtils.gzip(data);
    System.out.println("压缩之后大小:" + ret1.length);

    //测试解压缩
    byte[] ret2 = GzipUtils.ungzip(ret1);
    System.out.println("还原之后大小:" + ret2.length);

    //写出文件
    String writePath = System.getProperty("user.dir") + File.separatorChar
    +"receive" + File.separatorChar+"006.jpg";
    System.out.println(writePath);
    FileOutputStream fos = new FileOutputStream(writePath);
    fos.write(ret2);
    fos.close();
    }
    }
  • 相关阅读:
    react native的注释
    p标签在div中垂直居中,并且div高度随着p标签文字内容的变化而变化
    express框架搭建服务端
    react native项目的创建和运行
    ES6通过使用babel兼容到ie9
    react父传子
    经典排序之 归并排序
    经典排序之 冒泡排序
    C++基础题
    关于虚函数的应用(10个例子)
  • 原文地址:https://www.cnblogs.com/shmilyToHu/p/7131274.html
Copyright © 2011-2022 走看看