我正在使用 SharpZipLib 从数据库中解压缩一些 BLOB。
它不断抛出错误
"Unable to read from this stream"
在调用 Fill() 之前一切正常:
public void Fill()
{
rawLength = 0;
int toRead = rawData.Length;
while (toRead > 0) {
int count = inputStream.Read(rawData, rawLength, toRead);
if ( count <= 0 ) {
if (rawLength == 0) {
throw new SharpZipBaseException("Unexpected EOF");
}
break;
}
rawLength += count;
toRead -= count;
}
#if !NETCF_1_0
if ( cryptoTransform != null ) {
clearTextLength = cryptoTransform.TransformBlock(rawData, 0, rawLength, clearText, 0);
}
else
#endif
{
clearTextLength = rawLength;
}
available = clearTextLength;
}
在该行inputStream.Read(rawData, rawLength, toRead);
读取被称为:
public override int Read(byte[] buffer, int offset, int count)
{
if ( buffer == null ) {
throw new ArgumentNullException("buffer");
}
if ( offset < 0 ) {
#if NETCF_1_0
throw new ArgumentOutOfRangeException("offset");
#else
throw new ArgumentOutOfRangeException("offset", "Cannot be negative");
#endif
}
if ( count < 0 ) {
#if NETCF_1_0
throw new ArgumentOutOfRangeException("count");
#else
throw new ArgumentOutOfRangeException("count", "Cannot be negative");
#endif
}
if ( (buffer.Length - offset) < count ) {
throw new ArgumentException("Invalid offset/count combination");
}
return internalReader(buffer, offset, count);
}
但是,当 Read() 返回时internalReader(buffer, offset, count);
,它会转到以下错误捕获器:
int ReadingNotAvailable(byte[] destination, int offset, int count)
{
throw new InvalidOperationException("Unable to read from this stream");
}
我也将在 Sharp Development 论坛上发布这个问题。如果他们给出一个好的答案,我会发布一个链接。
以下是新方法。这些不是库的一部分。
public static byte[] DecompressNoHeader(byte[] bytInput)
{
var ms = new MemoryStream(bytInput, 0, bytInput.Length);
ZipInputStream zs = new ZipInputStream(ms);
byte[] bytResult = null;
string strResult = String.Empty;
var writeData = new byte[4096];
Stream s2 = new InflaterInputStream(zs, new Inflater(true));
try
{
bytResult = ReadFullStream(s2);
s2.Close();
return bytResult;
}
catch
{
throw;
}
}
public static byte[] ReadFullStream(Stream stream)
{
var buffer = new byte[32768];
using (var ms = new MemoryStream())
{
int offset = 0; // where were we
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, offset, read);
offset += read;
}
}
}