src/main/java/pw/phylame/util/ZLibUtils.java
/**
     * Compresses a specified area of input byte data with specified compression level.
     * @param data the input byte data
     * @param offset start index of compressing area
     * @param length length of compressing area
     * @param level ZLIB compression level
     * @return compressed data
     */
Compresses a specified area of input byte data with specified compression level.
Compresses a specified area of input byte data with specified compression level .
public static byte[] compress(byte[] data, int offset, int length, int level) {
        byte[] output = new byte[0];
        Deflater compresser = new Deflater(level);
        compresser.reset();
        compresser.setInput(data, offset, length);
        compresser.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
        try {
            byte[] buf = new byte[BUFFER_SIZE];
            while (!compresser.finished()) {
                int i = compresser.deflate(buf);
                bos.write(buf, 0, i);
            }
            output = bos.toByteArray();
        } catch (Exception e) {
            output = Arrays.copyOfRange(data, offset, offset+length);
            e.printStackTrace();
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        compresser.end();
        return output;
    }
public static byte [ ] compress ( byte [ ] data , int offset , int length , int level ) { byte [ ] output = new byte [ 0 ] ; Deflater compresser = new Deflater ( level ) ; compresser . reset ( ) ; compresser . setInput ( data , offset , length ) ; compresser . finish ( ) ; ByteArrayOutputStream bos = new ByteArrayOutputStream ( length ) ; try { byte [ ] buf = new byte [ BUFFER_SIZE ] ; while ( ! compresser . finished ( ) ) { int i = compresser . deflate ( buf ) ; bos . write ( buf , 0 , i ) ; } output = bos . toByteArray ( ) ; } catch ( Exception e ) { output = Arrays . copyOfRange ( data , offset , offset + length ) ; e . printStackTrace ( ) ; } finally { try { bos . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } compresser . end ( ) ; return output ; }
Compresses a specified area of input byte data with specified compression level.
Compresses a specified area of input byte data with specified compression level .