总算在C#.NET,Python,Ruby上实现了php的zlib的gzcompress函数

 

总算在C#.NET,Python,Ruby上实现了php的zlib的gzcompress函数

<?php
$s = "http://www.dc9.cn";
$z = gzcompress($s, 9);
echo $z;
$file_name = "dc9.cn.zip"; 
$file_pointer = fopen($file_name,"wb"); 
fwrite($file_pointer,$z); 
fclose($file_pointer);
@chmod($file_name,0777);
?>


别看上面这点代码东西少,后面蕴含的道理真不少。php用了zlib库里面的gzcompress函数,开始不知道这个函数是没有head data的,我傻不垃圾的弄了很多程序,最后都不成功,后来到www.php.net上面一查,原来如此,人家写着呢!!

This is not the same as gzip compression, which includes some header data. See gzencode() for gzip compression


gzcompress
(PHP 4 >= 4.0.1, PHP 5)

gzcompress -- Compress a string
说明
string gzcompress ( string data [, int level] )


This function compress the given string using the ZLIB data format. 

For details on the ZLIB compression algorithm see the document "ZLIB Compressed Data Format Specification version 3.3" (RFC 1950). 

注: This is not the same as gzip compression, which includes some header data. See gzencode() for gzip compression. 

难怪1字节的数字"1",压缩出来的玩意儿就9个字节,然而包含头数据的结果有21个字节,有时候又120个字节,有时候187个字节,有时候210个字节。当然是用不同的函数与方法压缩的,后面再说。反正9个字节那个才是对的。恩。
言归正传,如何在C#里面实现gzcompress压缩呢?
著名的压缩库ICSharpCode.SharpZipLib.GZip我是没有试出来,我认为他没有提供这种不包含头的压缩功能。我压缩出来的都是包含头的。于是我再次寻找到了zlib.net.dll这个是从http://www.zlib.net/官方网站找到的,果然什么东西都得用官方的!
那么代码就是这样的

            byte[] byteData = System.Text.Encoding.UTF8.GetBytes("http://www.dc9.cn");
            MemoryStream ms = new MemoryStream();
            Stream s = new ZOutputStream(ms, 9);
            
            s.Write(byteData, 0, byteData.Length);
            s.Close();
            byte[] compressData = (byte[])ms.ToArray();
            ms.Flush();
            ms.Close();

            FileStream fileStream = new FileStream("C://dc9.cn.zip", FileMode.Create, FileAccess.Write);
            Console.Write(System.Convert.ToBase64String(compressData, 0, compressData.Length));
            fileStream.Write(compressData, 0, compressData.Length);
            fileStream.Flush();
            fileStream.Close();


这次这个正确了,我突然很想尝试一下python压缩效果,这个没费多大劲。我觉得python就是简单,没得说,可惜不够普及。难怪人家google这么青睐他,我还想用lua试试,没准也很有意思,过两天再说了。。。。

这个是python的。

print
print 'Status: 200 OK'
print 'Content-type: text/html'
print
import zlib
import os, sys
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
f = open("dc9.cn.zip","wb")
deflate = zlib.compressobj(9)
buf = "http://www.dc9.cn";
sys.stdout.write(deflate.compress(buf))
sys.stdout.write(deflate.flush(zlib.Z_FINISH));
deflate2 = zlib.compressobj(9)
f.write(deflate2.compress(buf))
f.write(deflate2.flush())


写完这篇博客之后我又下载了ruby,然后也写了一段,现在把代码插在这里!
这个真他妈的简单。我有点爱上ruby了!!!!!!!!!!!!!!!!!!!!!!!!!
ruby比其他的多一个字节(回车)不过一样用。

z = Zlib::Deflate.new(9)
aFile = File.new("C://dc9.cn.txt","wb")
aFile.puts z.deflate("http://www.dc9.cn", Zlib::FINISH)
aFile.close
z.close


最后说说浪费时间的其他代码,放在这别浪费了。

我最先想到的是.net framework2.0以来新加入的一个System.IO.Compression;这里面的GZipStream 不提供level,不提供无头压缩。很郁闷。这个压缩出来是直接能用winrar解压的。带头的才能winrar/winzip解压。

            FileStream fileStream = new FileStream("C://dc9.cn.zip",FileMode.Create,FileAccess.Write);
            MemoryStream ms = new MemoryStream();
            GZipStream compressionStream = new GZipStream(ms,CompressionMode.Compress);
            StreamWriter writer = new StreamWriter(compressionStream);
            writer.Write("http://www.dc9.cn");
            writer.Close();

            fileStream.Write(ms.ToArray(), 0, ms.ToArray().Length);
            fileStream.Flush();
            fileStream.Close();


using ICSharpCode.SharpZipLib.GZip;这个我懒得说了,也是带头的
关键就是

FileStream destinationStream = new FileStream("C://dc9.cn.zip",FileMode.OpenOrCreate);
GZipOutputStream outStream = new GZipOutputStream(destinationStream);
outStream.Write(buffer, 0, buffer.Length);
outStream.Flush();
outStream.Finish();


就这些了。
什么?有什么用?
不带头东东在网络中传输不是更快么?对吧。嘻嘻。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值