java 压缩

GZIP

  • https://nowjava.com/book/java-example/11374
  • Java GZIP Example – Compress and Decompress File
  • java GZIP压缩与解压缩
  • https://blog.csdn.net/wenqisun/article/details/51121460
  • string
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public static byte[] compress(String str, String encoding) {
    if (str == null || str.length() == 0) {
    return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip;
    try {
    gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes(encoding));
    gzip.close();
    } catch ( Exception e) {
    e.printStackTrace();
    }
    return out.toByteArray();
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public static byte[] uncompress(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
    return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    try {
    GZIPInputStream ungzip = new GZIPInputStream(in);
    byte[] buffer = new byte[256];
    int n;
    while ((n = ungzip.read(buffer)) >= 0) {
    out.write(buffer, 0, n);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return out.toByteArray();
    }
  • file
  • compress-a-file-in-gzip-format-in-java/
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.GZIPOutputStream;

    public class CompressFileGzip {

    public static void main(String[] args) {


    String source_filepath = "C:\\Users\\nikos7\\Desktop\\files\\test.txt";
    String destinaton_zip_filepath = "C:\\Users\\nikos7\\Desktop\\files\\test.gzip";

    CompressFileGzip gZipFile = new CompressFileGzip();
    gZipFile.gzipFile(source_filepath, destinaton_zip_filepath);
    }

    public void gzipFile(String source_filepath, String destinaton_zip_filepath) {

    byte[] buffer = new byte[1024];

    try {

    FileOutputStream fileOutputStream =new FileOutputStream(destinaton_zip_filepath);

    GZIPOutputStream gzipOuputStream = new GZIPOutputStream(fileOutputStream);

    FileInputStream fileInput = new FileInputStream(source_filepath);

    int bytes_read;

    while ((bytes_read = fileInput.read(buffer)) > 0) {
    gzipOuputStream.write(buffer, 0, bytes_read);
    }

    fileInput.close();

    gzipOuputStream.finish();
    gzipOuputStream.close();

    System.out.println("The file was compressed successfully!");

    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }

    }
  • compressing-decompressing-files-using-gzip-format-java/
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.GZIPInputStream;

    class GeeksForGeeks
    {
    static final String INPUT_FILE
    = "/home/saket/Desktop/GeeksforGeeks/compress.gz";
    static final String OUTPUT_FILE
    = "/home/saket/Desktop//GeeksforGeeks/decompress.java";

    static void decompress()
    {
    byte[] buffer = new byte[1024];
    try
    {
    GZIPInputStream is =
    new GZIPInputStream(new FileInputStream(INPUT_FILE));

    FileOutputStream out =
    new FileOutputStream(OUTPUT_FILE);

    int totalSize;
    while((totalSize = is.read(buffer)) > 0 )
    {
    out.write(buffer, 0, totalSize);
    }

    out.close();
    is.close();

    System.out.println("File Successfully decompressed");
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }

    public static void main (String[] args)
    {
    decompress();

    }
    }

    Deflater / Inflater

  • https://cloud.tencent.com/developer/article/1673594
  • https://www.cnblogs.com/zohnn/p/12899931.html
  • JDK 之 Deflater 压缩与 Inflater 解压
  • 关于Inflater和Deflater的简单用法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.Random;
    import java.util.zip.DataFormatException;
    import java.util.zip.Deflater;
    import java.util.zip.Inflater;

    public class DeflaterDemo2 {
    public static void main(String[] args) throws DataFormatException, IOException {
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < 100000; i++) {
    builder.append("Welcome to TutorialsPoint.com" + (new Random().nextInt() * 26));
    }
    String text = builder.toString();
    byte[] compres = compress(text.getBytes(), true);
    System.out.println(compres.length + " : " + text.getBytes().length);
    String res = uncompress(compres, true);
    System.out.println(res.equals(text));

    byte[] compres2 = compress(text.getBytes(), false);
    System.out.println(compres2.length + " : " + text.getBytes().length);
    String res2 = uncompress(compres2, false);
    System.out.println(res2.equals(text));
    }

    public static String uncompress(byte[] input, boolean nowrap) throws IOException {
    Inflater inflater = new Inflater(nowrap);
    inflater.setInput(input);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
    try {
    byte[] buff = new byte[1024];
    while (!inflater.finished()) {
    int count = inflater.inflate(buff);
    baos.write(buff, 0, count);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    baos.close();
    }
    inflater.end();
    byte[] output = baos.toByteArray();
    return new String(output);
    }

    public static byte[] compress(byte[] data, boolean nowrap) throws IOException {
    byte[] output;
    Deflater compress = new Deflater(Deflater.DEFAULT_COMPRESSION, nowrap);

    compress.reset();
    compress.setInput(data);
    compress.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    try {
    byte[] buf = new byte[1024];
    while (!compress.finished()) {
    int i = compress.deflate(buf);
    bos.write(buf, 0, i);
    }
    output = bos.toByteArray();
    } catch (Exception e) {
    output = data;
    e.printStackTrace();
    } finally {
    bos.close();
    }
    compress.end();
    return output;
    }

    }

    ZIP

  • Zipping and Unzipping in Java

org.apache.commons.codec.binary.Base64

  • Java字符串的压缩与解压缩的两种方法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    /**
    * 使用org.apache.commons.codec.binary.Base64压缩字符串
    * @param str 要压缩的字符串
    * @return
    */
    public static String compress(String str) {
    if (str == null || str.length() == 0) {
    return str;
    }//加入Java开发交流君样:756584822一起吹水聊天
    return Base64.encodeBase64String(str.getBytes());
    }

    /**
    * 使用org.apache.commons.codec.binary.Base64解压缩
    * @param compressedStr 压缩字符串
    * @return
    */
    public static String uncompress(String compressedStr) {
    if (compressedStr == null) {
    return null;
    }
    return Base64.decodeBase64(compressedStr);
    }
    注意事项

在web项目中,服务器端将加密后的字符串返回给前端,前端再通过ajax请求将加密字符串发送给服务器端处理的时候,在http传输过程中会改变加密字符串的内容,导致服务器解压压缩字符串发生异常:

java.util.zip.ZipException: Not in GZIP format
解决方法:

在字符串压缩之后,将压缩后的字符串BASE64加密,在使用的时候先BASE64解密再解压即可。

参考文章

评论