我有这个函数,它根据最大长度将一个序列化的字节数组分成几部分:
public static List<byte[]> ZipStringList(List<string> curSpawn, int maxSize = -1) {
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
MemoryStream o = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(o, curSpawn);
byte[] buffer = o.GetBuffer();
o.Close();
MemoryStream memoryStream = new MemoryStream();
using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}
memoryStream.Position = 0;
if (maxSize > 0 && memoryStream.Length > maxSize) {
int pieces = Mathf.CeilToInt (memoryStream.Length / (float)maxSize);
List<byte[]> listByte = new List<byte[]> ();
for (int i = 0; i < pieces; ++i) {
int curLength = (int)Mathf.Min (maxSize, memoryStream.Length - (i * maxSize));
byte[] compressedData = new byte[curLength];
memoryStream.Read (compressedData, 0, compressedData.Length);
if (i == pieces - 1) {
byte[] gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy (compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy (BitConverter.GetBytes (buffer.Length), 0, gZipBuffer, 0, 4);
listByte.Add (gZipBuffer);
}//if last piece
else {
listByte.Add (compressedData);
}//else normal
}//for each piece
return listByte;
} else {
byte[] compressedData = new byte[memoryStream.Length];
memoryStream.Read (compressedData, 0, compressedData.Length);
byte[] gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy (compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy (BitConverter.GetBytes (buffer.Length), 0, gZipBuffer, 0, 4);
List<byte[]> retList = new List<byte[]> ();
retList.Add (gZipBuffer);
return retList;
}
}//ZipStringList()
然后这个反序列化它的函数:
public static List<string> UnzipStringList(List<byte[]> byteList) {
byte[] gZipBuffer = byteList.SelectMany(b => b).ToArray();
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
byte[] buffer;
using (MemoryStream memoryStream = new MemoryStream())
{
int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
buffer = new byte[dataLength];
memoryStream.Position = 0;
using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
gZipStream.Read(buffer, 0, buffer.Length);
}
}
MemoryStream o = new MemoryStream(buffer);
BinaryFormatter bf = new BinaryFormatter();
return (List<string>)bf.Deserialize(o);
}//UnzipStringList()
当我运行它时,我得到一个&#34;损坏的gzip标头&#34;错误,所以我错过了一些简单的事情。它是什么?
更多细节:实际上,我可能错误地定位了缓冲区。有趣的事实:我搜索了谷歌的错误消息,唯一的结果是这个问题。
这是您可以测试的一些代码。使它足够大(至少一千字节)。我的文件充满了JSON数组。
string[] allLines = File.ReadAllLines ("path_to_file.txt");
List<byte[]> byteArray = Utility.ZipStringList(new List<string>(allLines), 60000);
List<string> stringList = Utility.UnzipStringList (byteArray);
Utility.LogRealError ("I unzipped it?", stringList.Count, stringList [4]);