java_zlib_资料

Posted JavaSkill

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java_zlib_资料相关的知识,希望对你有一定的参考价值。

1、网页资料

  1.1、http://bbs.csdn.net/topics/190020986

  1.2、

    http://cdn.verydemo.com/demo_c89_i166794.html

    http://blog.csdn.net/hexingyeyun/article/details/8678154

  1.3、

    http://www.lai18.com/content/2088054.html

    http://www.jb51.net/article/95073.htm

 

  1.4、Delphi external 使用 ?.obj中的函数:http://bbs.csdn.net/topics/60347700

2、我的测试代码

  2.1、

procedure TForm1.Button2Click(Sender: TObject);
var cs :TCompressionStream;
    ms :TMemoryStream;
    pc :PChar;
    buffer :PCHar;
    i :integer;
begin
  pc := Test123456;
  ms := TMemoryStream.Create;
  cs := TCompressionStream.Create(clMax, ms); // (clNone, clFastest, clDefault, clMax);
  cs.Write(pc[0], strlen(pc));
  cs.Free;
  Memo1.Lines.Add(-->+inttostr(ms.size));

  GetMem(buffer, ms.size);
  ms.Read(buffer[0], ms.size);

  for i:=0 to ms.size do
    Memo1.Lines.Add(inttostr(integer(buffer[i])));
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  zip: TVclZip;
  compr: string;
  data: PChar;
  txt :PChar;
  arr :array[0..256] of byte;
  iOut :integer;
  p :pointer;
  i :integer;
begin
  txt := Test123456;
  iOut := 0;
  zip := TVclZip.Create(nil);
  for i:=0 to 17 do
    Memo1.Lines.Add(inttostr(integer(arr[i])));
    
  p := nil;
  zip.ZLibCompressBuffer(txt, strlen(txt), p, iOut);
  zip.Free;

  Memo1.Lines.Add(-->+inttostr(iOut));

  for i:=0 to iOut-1 do
    Memo1.Lines.Add(inttostr(integer(PChar(p)[i])));
end;

  2.2、

package str;

import java.io.ByteArrayOutputStream;
import java.sql.ResultSet;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.InflaterOutputStream;

public class TstrTest
{
    public static void main(String[] args) throws Exception
    {
//        String str = "退出";
//        
//        byte[] bytesGBK = str.getBytes("gbk");
//        for (int i=0; i<bytesGBK.length; i++)
//            System.out.print(String.format("%02X ", bytesGBK[i]));
//        System.out.println();
//        
//        byte[] bytesUTF8 = str.getBytes("utf-8");
//        for (int i=0; i<bytesUTF8.length; i++)
//            System.out.print(String.format("%02X ", bytesUTF8[i]));
        
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        //InflaterOutputStream ios = new InflaterOutputStream(bao);
        DeflaterOutputStream dos = new DeflaterOutputStream(bao);
        String str = "Test123456";
        dos.write(str.getBytes("iso-8859-1"));
        dos.close();
        byte[] bytes = bao.toByteArray();
        
        for (int i=0; i<bytes.length; i++)
            System.out.println(i+" : " + ((int)bytes[i] & 0xFF));
        //SevenZArchiveEntry ss;
        
        System.out.println();
        bytes = Compress(str.getBytes("iso-8859-1"));
        for (int i=0; i<bytes.length; i++)
            System.out.println(i+" : " + ((int)bytes[i] & 0xFF));
    }
    
    public static byte[] Compress(byte[] _bytes) throws Exception
    {
        if (_bytes == null || _bytes.length == 0)
            return null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(_bytes);
        gzip.close();
        return out.toByteArray();
    }
}

    ZC: 仔细对比 java代码中 zlib和gzip 压缩出来的数据 其实是一样的,只是 头部&尾部不同而已,也印证了上面 网页资料1.3 中的说法

3、

 

以上是关于java_zlib_资料的主要内容,如果未能解决你的问题,请参考以下文章

985高校的高材生只会写代码片段,丢人吗?

985大学的高材生只会写代码片段,丢人吗?

985大学的高材生只会写代码片段,丢人吗?

985大学的高材生只会写代码片段,丢人吗?

这些 C++ 代码片段有啥作用?

[AndroidStudio]_[初级]_[配置自动完成的代码片段]