我在使用SharpZipLib库(运行时版本: v2.0.50727)的BZip2算法编写静态Deflate扩展方法时遇到了麻烦,我将用它来压缩字符串。
我使用的是.NET Framework4。
这是我的VB.NET代码:
Public Function Deflate(ByVal text As String)
Try
Dim compressedData As Byte() = Convert.FromBase64String(text)
System.Diagnostics.Debug.WriteLine(String.Concat("Compressed text data size: ", text.Length.ToString()))
System.Diagnostics.Debug.WriteLine(String.Concat("Compressed byte data size: ", compressedData.Length.ToString()))
Using compressedStream As MemoryStream = New MemoryStream(compressedData)
Using decompressionStream As BZip2OutputStream = New BZip2OutputStream(compressedStream)
Dim cleanData() As Byte
Using decompressedStream As MemoryStream = New MemoryStream()
decompressionStream.CopyTo(decompressedStream) // HERE THE ERROR!
cleanData = decompressedStream.ToArray()
End Using
decompressionStream.Close()
compressedStream.Close()
Dim cleanText As String = Encoding.UTF8.GetString(cleanData, 0, cleanData.Length)
System.Diagnostics.Debug.WriteLine(String.Concat("After decompression text data size: ", cleanText.Length.ToString()))
System.Diagnostics.Debug.WriteLine(String.Concat("After decompression byte data size: ", cleanData.Length.ToString()))
Return cleanText
End Using
End Using
Catch
Return String.Empty
End Try
End Function奇怪的是,我编写了一个与相同方法对应的C#,并且它完美地工作了!代码如下:
public static string Deflate(this string text)
{
try
{
byte[] compressedData = Convert.FromBase64String(text);
System.Diagnostics.Debug.WriteLine(String.Concat("Compressed text data size: ", text.Length.ToString()));
System.Diagnostics.Debug.WriteLine(String.Concat("Compressed byte data size: ", compressedData.Length.ToString()));
using (MemoryStream compressedStream = new MemoryStream(compressedData))
using (BZip2InputStream decompressionStream = new BZip2InputStream(compressedStream))
{
byte[] cleanData;
using (MemoryStream decompressedStream = new MemoryStream())
{
decompressionStream.CopyTo(decompressedStream);
cleanData = decompressedStream.ToArray();
}
decompressionStream.Close();
compressedStream.Close();
string cleanText = Encoding.UTF8.GetString(cleanData, 0, cleanData.Length);
System.Diagnostics.Debug.WriteLine(String.Concat("After decompression text data size: ", cleanText.Length.ToString()));
System.Diagnostics.Debug.WriteLine(String.Concat("After decompression byte data size: ", cleanData.Length.ToString()));
return cleanText;
}
}
catch(Exception e)
{
return String.Empty;
}
}在VB.NET版本中,我得到这个错误:“流不支持读取。”(请参阅代码以了解它的来历!)
错误在哪里?!!我不明白这两种方法有什么不同。
非常感谢!
发布于 2012-08-24 22:17:26
点对点游戏的区别表明,在第一个游戏中,您使用的是BZip2OutputStream,而第二个游戏是BZip2InputStream。
输出流是用来写入的,这似乎是合理的,因此它说是不可读的。
不管它有什么价值,有很多好的比较工具。它们不会处理不同的语法,但当你使用完全不同的对象时(至少在这种情况下),匹配的方式会很好地显示出来。我个人使用并推荐使用的是Beyond Compare
发布于 2012-08-24 22:17:33
你换了BZip2OutputStream和BZip2InputStream
发布于 2012-08-24 22:17:41
在一个版本中,您使用BZip2InputStream,而在另一个版本中,您使用BZip2OutputStream。
https://stackoverflow.com/questions/12111023
复制相似问题