为什么先压缩然后再压缩不同长度的流
我正在使用SevenZipSharp库进行压缩,然后解压缩包含简单序列化对象的MemoryStream。但是,压缩流和解压缩流的长度不同。
从下面运行的代码中我得到
输入长度:174
输出长度:338
(SevenZipSharp dll被包含作为参考,而7z.dll被包含在项目输出中)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace DataTransmission {
class Program {
static void Main(string[] args)
{
SevenZip.SevenZipCompressor compressor = new SevenZip.SevenZipCompressor();
//compressor.CompressionMethod = SevenZip.CompressionMethod.Lzma2;
//compressor.CompressionLevel = SevenZip.CompressionLevel.Normal;
MemoryStream inputStream = new MemoryStream();
Person me = new Person(\"John\", \"Smith\");
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(inputStream, me);
Int32 inputStreamLength = (Int32)inputStream.Length;
MemoryStream outputStream = new MemoryStream();
compressor.CompressStream(inputStream, outputStream);
SevenZip.SevenZipExtractor decompressor = new SevenZip.SevenZipExtractor(outputStream);
decompressor.ExtractFile(0, outputStream);
Int32 outputStreamLength = (Int32)outputStream.Length;
Console.WriteLine(\"Input length: {0}\", inputStreamLength);
Console.WriteLine(\"Output length: {0}\", outputStreamLength);
Console.ReadLine();
}
}
[Serializable]
public class Person {
public string firstName;
public string lastName;
public Person(string fname, string lname) {
firstName = fname;
lastName = lname;
}
}
}
谁能帮助我解决这个问题?
谢谢,
没有找到相关结果
已邀请:
2 个回复
缝皋
。 (实际上,这很奇怪,因为解压缩器正在从
读取并且还在写入
。这是个坏主意。请使用两个不同的流。) 您还应在写入每个流之后且在其他内容想要读取它之前倒回每个流,例如与
在这种情况下,SevenZipLib可能会为您执行此操作,但是通常,如果您希望从流的开始就执行某些操作,则应适当地重置它。 我刚刚对您的代码进行了以下更改,这时我得到的输入和输出长度相同:
正如我所说,您也应该进行其他适当的更改。
部窖空
您正在从outputStream解压缩为outputStream。较大的数据可能会失败。进行更改,使其读取