将压缩内存流保存到文件流,结束文件已损坏
Saving compressed memory stream to filestream , endfile is corrupted
我正在使用 seven.zip.sharp 压缩流。然后我想在压缩完成后,将内存流中的数据保存到文件流中。该文件是一个“.7z”文件。
问题:
输出文件已损坏,我无法手动解压缩。使用记事本++我也看不到通常在7zip文件中找到的header。
代码如下:
//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-Zipz.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");
}
请有人更改问题以使其看起来更好。如果您愿意,可以添加更好的标题。谢谢。
因此您计划将压缩流存储在临时 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;
我正在使用 seven.zip.sharp 压缩流。然后我想在压缩完成后,将内存流中的数据保存到文件流中。该文件是一个“.7z”文件。
问题:
输出文件已损坏,我无法手动解压缩。使用记事本++我也看不到通常在7zip文件中找到的header。
代码如下:
//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-Zipz.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");
}
请有人更改问题以使其看起来更好。如果您愿意,可以添加更好的标题。谢谢。
因此您计划将压缩流存储在临时 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;