我正在使用seven.zip.sharp压缩流。然后,在压缩完成后,将内存流中的数据保存到filestream中。该文件是".7z“文件。
问题:
输出文件已损坏,我无法手动解压它。使用notepad++,我也无法看到通常在7zip文件中找到的头部。
以下是代码:
//Memory stream used to store compressed stream
public System.IO.Stream TheStream = new System.IO.MemoryStream();
//Start compress stream
private void button1_Click(object sender, EventArgs e)
{
Thread newThread1 = new Thread(this.COMP_STREAM);
newThread1.Start();
}
//See size of stream on demand
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox1.Text += "-" + TheStream.Length;
}
//To Create file
private void button3_Click(object sender, EventArgs e)
{
byte[] buffer = new byte[1024]; // Change this to whatever you need
using (System.IO.FileStream output = new FileStream(@"F:\Pasta desktop\sss\TESTEmiau.7z", FileMode.Create))
{
int readBytes = 0;
while ((readBytes = TheStream.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, readBytes);
}
output.Close();
}
MessageBox.Show("DONE");
}
//To compress stream
public void COMP_STREAM()
{
SevenZip.SevenZipCompressor.SetLibraryPath(@"C:\Program Files\7-Zip\7z.dll");
var stream = System.IO.File.OpenRead(@"F:\Pasta desktop\sss\lel.exe");
SevenZip.SevenZipCompressor compressor = new SevenZip.SevenZipCompressor();
compressor.CompressionMethod = SevenZip.CompressionMethod.Lzma2;
compressor.CompressionLevel = SevenZip.CompressionLevel.Ultra;
compressor.CompressStream(stream, TheStream); //I know i can just use a FileStream here but i am doing this from testing only.
MessageBox.Show("Done");
}请有人修改这个问题,使它看起来更好。如果你想要增加更好的标题。谢谢。
发布于 2016-03-29 15:24:11
因此,您计划将压缩的流存储在临时的MemoryBuffer中,然后将其写入文件中。问题是MemoryStream必须在写入后重置,所以读取操作从一开始就读取。如果输出文件大小为0,那么我很肯定这就是问题所在。
下面是一个解决办法:
// Seek the beginning of the `MemoryStrem` before writing it to a file:
TheStream.Seek(0, SeekOrigin.Begin);也可以将流声明为MemoryStream,并使用Position属性:
TheStream.Position = 0;https://stackoverflow.com/questions/36287640
复制相似问题