1 回答
TA贡献1820条经验 获得超9个赞
我用 gzipstream 做了类似的事情。但这完全取决于您希望如何打包文件。
在这种情况下,这样的事情将是理想的。
string payload = Path.GetTempFileName();
using (FileStream temp_fs = new FileStream(payload, FileMode.OpenOrCreate))
{
using (BinaryWriter output_s = new BinaryWriter(new FileStream("target.out", FileMode.OpenOrCreate)))
{
//Write a blank. must be a long!
output_s.Write((long)0);
foreach (string file in Files)
{
//Write the files name
output_s.Write(file);
long start = temp_fs.Position;
//Write the starting point
output_s.Write(start);
//Compress the file to the payload
using (GZipStream gzip = new GZipStream(temp_fs, CompressionMode.Compress, true))
{
using (FileStream fs = new FileStream(file, FileMode.Open))
{
fs.CopyTo(gzip);
}
}
//Write the length
output_s.Write(temp_fs.Position - start);
}
//When all files are written
//Get the size of our header
long headersize = output_s.BaseStream.Length - 8;
//Copy the temp file data to the end
temp_fs.CopyTo(output_s.BaseStream);
//Reset to the start of the stream
output_s.BaseStream.Position = 0;
//override our zero
output_s.Write(headersize);
}
}
File.Delete(payload);
当您读取文件时,将进行二进制读取,您将能够按名称获取文件,然后在文件名之后的下一个很长的时间将是其大小并开始。然后你将节头+pos解压缩到长度。检索您的文件。
- 1 回答
- 0 关注
- 181 浏览
添加回答
举报