Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL Compress解压函数的Java仿真_Java_Mysql_Compression_Percona - Fatal编程技术网

MySQL Compress解压函数的Java仿真

MySQL Compress解压函数的Java仿真,java,mysql,compression,percona,Java,Mysql,Compression,Percona,我想在mysql VARBINARY列中插入一些字节数据。数据很大,所以我想以压缩的方式存储它 我使用的是Percona5.6MySQL。我想在Java中模拟mysql的压缩功能,然后将结果插入数据库。 我想使用MySql解压功能来访问这些数据。 有办法吗 我尝试过使用标准java.zip包。但它不起作用 编辑。用不同的措辞,PHP的gzcompress(ZLIB)的Java等价物是什么?压缩的结果是未压缩数据的四字节小尾端长度,然后是包含压缩数据的ZLIB流 您可以使用Java中的Deflat

我想在mysql VARBINARY列中插入一些字节数据。数据很大,所以我想以压缩的方式存储它

我使用的是Percona5.6MySQL。我想在Java中模拟mysql的压缩功能,然后将结果插入数据库。 我想使用MySql解压功能来访问这些数据。 有办法吗

我尝试过使用标准java.zip包。但它不起作用


编辑。用不同的措辞,PHP的
gzcompress
(ZLIB)的Java等价物是什么?

压缩的结果是未压缩数据的四字节小尾端长度,然后是包含压缩数据的ZLIB流


您可以使用Java中的
Deflater
类压缩到zlib流。在结果前面加上四个字节的长度。

解决方案:实现MYSQL压缩和解压缩

//Compress byte stream using ZLib compression
  public static byte[] compressZLib(byte[] data)  {
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    deflater.finish();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
      int count = deflater.deflate(buffer); // returns the generated code... index
      outputStream.write(buffer, 0, count);
    }
    try {
      outputStream.close();
    } catch (IOException e) {
    }
    return outputStream.toByteArray();
  }

//MYSQL COMPRESS.
  public static byte[] compressMySQL(byte[] data)  {
    byte[] lengthBeforeCompression = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).order(ByteOrder.LITTLE_ENDIAN).putInt(data.length).array();
    ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
    try {
      resultStream.write( lengthBeforeCompression );
      resultStream.write( compressZLib(data));
      resultStream.close();
    } catch (IOException e) {
    }
    return resultStream.toByteArray( );
  }

//Decompress using ZLib
  public static byte[] decompressZLib(byte[] data) {
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    try {
      while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
      }
      outputStream.close();
    }catch (IOException ioe) {
    } catch (DataFormatException e) {
    }
    return outputStream.toByteArray();
  }

  //MYSQL DECOMPRESS
  public static byte[] decompressSQLCompression(byte[] input) {
    //ignore first four bytes which denote length of uncompressed data. use rest of the array for decompression
    byte[] data= Arrays.copyOfRange(input,4,input.length);
    return decompressZLib(data);
  }

你什么意思它不起作用?添加一些东西来帮助发现答案。为什么?让数据库来做。这就是它的用途。谢谢马克。我缺少的是以little endian格式存储的未压缩数据的四字节长度。它工作得很好!