如何将大文件打包到 Tar 中?
如果您使用此代码,则会使用大量 RAM。
using (TarWriter tarwriter = new(_writestream, TarEntryFormat.Pax, leaveOpen: false))
using (FileStream srcFile = new(item, FileMode.Open, FileAccess.Read))
{
string fileName = item.Remove(0, mainDir.Length + 1).Replace('\\', '/');
PaxTarEntry te = new(TarEntryType.RegularFile, fileName)
{
DataStream = new MemoryStream()
};
int currentBlockSize = 0;
while ((currentBlockSize = srcFile.Read(buffer)) > 0)
{
te.DataStream.Write(buffer.AsSpan(0, currentBlockSize));
}
te.DataStream.Position = 0;
tarwriter.WriteEntry(te);
}
使用System.IO; 使用 System.IO.压缩;
public void PackLargeFileIntoTar(字符串sourceFilePath,字符串tarFilePath) { 使用 (FileStream tarFileStream = new FileStream(tarFilePath, FileMode.Create, FileAccess.Write)) 使用 (TarArchive tarArchive = TarArchive.Create()) { tarArchive.WriteTo(tarFileStream, TarEntryFormat.Pax, leftOpen: false);
using (FileStream srcFile = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read))
{
string fileName = Path.GetFileName(sourceFilePath);
var entry = TarEntry.CreateEntryFromFile(sourceFilePath);
tarArchive.AddEntry(fileName, srcFile);
}
}
}