没有指定名称字段的编码,任何非 ASCII 字节都将被丢弃

No Encoding for Name field is specified, any non-ASCII bytes will be discarded

以下 .NET 5.0 代码使用 ICSharpCode.SharpZipLib

var gzipInputStream = new GZipInputStream(sourceStream);
var tarInputStream = new TarInputStream(gzipInputStream);

var gZipOutputStream = new GZipOutputStream(destinationStream);
var tarOutputStream = new TarOutputStream(gZipOutputStream);

现在发出警告

[CS0618] 'TarInputStream.TarInputStream(Stream)' is obsolete: 
    'No Encoding for Name field is specified, any non-ASCII bytes will be discarded'

[CS0618] 'TarOutputStream.TarOutputStream(Stream)' is obsolete: 
    'No Encoding for Name field is specified, any non-ASCII bytes will be discarded'

构造TarInputStreamTarOutputStream时应该指定什么Encoding

您指定的编码取决于文件的内容,并且取决于您在您的方案中尝试achieve\support的内容。

因为看起来默认是 ASCII,实际上你现在不需要 'need' 到 change\specify 任何编码。

关于过时的标志警告,如果您询问如何处理警告并保持默认编码,您可以使用 TarOutputStream.TarOutputStream(Stream, null) ctor 方法签名。


更新(参考维护者的评论以及对 Github 问题的回复)

TarOutputStream.TarOutputStream(Stream, null)中指定null时条目编码过程的默认行为是

no encoding conversion / just [copies] the lower 8 bits

关于指定编码的建议:

If you don't know what encoding might have been used, the safest bet is often to specify UTF-8

因此,我的建议与该建议相呼应。您调用非过时的构造函数并指定 Encoding.UTF8.

var gzipInputStream = new GZipInputStream(sourceStream);
var tarInputStream = new TarInputStream(gzipInputStream, Encoding.UTF8);

var gZipOutputStream = new GZipOutputStream(destinationStream);
var tarOutputStream = new TarOutputStream(gZipOutputStream, Encoding.UTF8);

谢谢@piksel bitworks 和@Panagiotis Kanavos