DeflateStream类的Read方法怎么才能不出现“数据流的格式无效”错误
【求助】DeflateStream类的Read方法如何才能不出现“数据流的格式无效”异常
请问,传入的数据流到底要什么格式?
我这里就只传了一串字符(A-Z)
------解决方案--------------------
- C/C++ code
#include <iostream> #include <cstdlib> #using <System.dll> using namespace std; using namespace System; using namespace System::IO; using namespace System::IO::Compression; array<unsigned char> ^DecompressData(array<unsigned char> ^CompressedBytes); int main() { array<unsigned char> ^result, ^buffer; int i; buffer = gcnew array<unsigned char>(26); for (i = 0; i < 26; ++i) buffer[i] = (unsigned char)(i + 65); for (i = 0; i < 26; ++i) cout << buffer[i] << '\t'; cout << endl; result = DecompressData(buffer); if (result[0] != 0) { for (i = 0; i < result->Length; ++i) cout << result[i] << '\t'; } cout << endl; system("pause"); return 0; } array<unsigned char> ^DecompressData(array<unsigned char> ^CompressedBytes) { MemoryStream ^Memory; DeflateStream ^Unzip; array<unsigned char> ^temp; array<unsigned char> ^DecompressBytes; int length; try { Memory = gcnew MemoryStream(); Memory->Position = 0; temp = gcnew array<unsigned char>(1024); Unzip = gcnew DeflateStream(gcnew MemoryStream(CompressedBytes), CompressionMode::Decompress, false); length = Unzip->Read(temp, 0, 4); Unzip->Flush(); length = 0; do { length = Unzip->Read(temp, 0, temp->Length); Memory->Write(temp, 0, length); Unzip->Flush(); } while (length != 0); Unzip->Close(); DecompressBytes = Memory->ToArray(); Console::WriteLine(DecompressBytes); Memory->Close(); return DecompressBytes; } catch (ArgumentNullException ^) { Console::WriteLine("array为null引用"); } catch (ObjectDisposedException ^) { Console::WriteLine("流已关闭。"); } catch (InvalidOperationException ^) { Console::WriteLine("创建对象时CompressionMod值是Compress。基础流不支持读取。"); } catch (ArgumentOutOfRangeException ^) { Console::WriteLine("offset或count小于零。array长度减去索引起始点小于count。"); } catch (InvalidDataException ^) { Console::WriteLine("数据的格式无效。"); } catch (...) { Console::WriteLine("Something else went wrong in function DecompressData()"); } return gcnew array<unsigned char>(CompressedBytes->Length); }
请问,传入的数据流到底要什么格式?
我这里就只传了一串字符(A-Z)
------解决方案--------------------