▲▲▲▲ZLib压缩字符串
想用ZLib压缩字符串,从网上下了一个组件,压缩文件好用,压缩字符串好象字符如果很多了就成功,如果比较小就几个或几十个就不会成功,晕死了,谁能给个Zlib的示例及组件,我不想用微软提供的,因为那个实在是一般,测试过用微软压exe文件压缩后反而会大很多,晕啊.
我的邮箱zsgbox@gmail.com
麻烦做过的达人了.
------解决方案--------------------压缩前检测一下文件大小,如果小于100字节就不压缩。
------解决方案--------------------我用的是SharpZipLib,VBNET的代码
文件压缩
Public Function Compress(ByVal fileName As String, ByVal folder As String) As Boolean
If Me._zipOutStream Is Nothing Then Return False
Dim isSucceeded As Boolean = True
Dim inStream As Stream
Try
Dim buff As Byte() = New Byte(16 * 1024) {}
Dim len As Int32 = 0
Dim entryName As String = System.IO.Path.GetFileName(fileName)
inStream = New BufferedStream(New FileStream(fileName,
FileMode.Open,FileAccess.Read, FileShare.Read), 32 * 1024)
Dim ze As New ICSharpCode.SharpZipLib.Zip.ZipEntry(folder & entryName)
ze.Size = inStream.Length
Me._zipOutStream.PutNextEntry(ze)
len = inStream.Read(buff, 0, buff.Length)
While (len > 0)
Me._zipOutStream.Write(buff, 0, len)
len = inStream.Read(buff, 0, buff.Length)
End While
Me._zipOutStream.Flush()
Catch ex As Exception
isSucceeded = False
Me.CloseWrite()
Finally
If Not inStream Is Nothing Then inStream.Close()
End Try
Return isSucceeded
End Function
文件解压
Public Function UnCompress(ByVal folderName As String) As Boolean
If Me._zipInStream Is Nothing Then Return False
Dim isSucceeded As Boolean = True
Dim outStream As Stream
Dim ze As ICSharpCode.SharpZipLib.Zip.ZipEntry
Try
Dim buff As Byte() = New Byte(16 * 1024) {}
Dim len As Int32 = 0
Dim fs As FileStream
ze = Me._zipInStream.GetNextEntry()
While Not ze Is Nothing
fs = New FileStream(folderName & ze.Name, FileMode.Create, FileAccess.Write, FileShare.Write)
outStream = New BufferedStream(fs, 32 * 1024)
len = Me._zipInStream.Read(buff, 0, buff.Length)
While len > 0
outStream.Write(buff, 0, len)
len = Me._zipInStream.Read(buff, 0, buff.Length)
End While
outStream.Close()
ze = Me._zipInStream.GetNextEntry()
End While
Catch ex As Exception
Finally
If Not outStream Is Nothing Then outStream.Close()
End Try
Return isSucceeded
End Function
------解决方案--------------------估计是你字节太小,达不到它写的信息的大小吧
------解决方案--------------------C# code
int.Parse(OutStream.Length.ToString())