<?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.
言归正传,如何在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();
就这些了。
什么?有什么用?
不带头东东在网络中传输不是更快么?对吧。嘻嘻。