Search code examples
c#.net.net-coresharpziplib

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


The following .NET 5.0 code using ICSharpCode.SharpZipLib

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

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

now emits warnings

[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'

What Encoding should I specify when constructing TarInputStream and TarOutputStream?


Solution

  • The encoding you specify is dependent on the contents of the file, and is subject to what you are trying to achieve\support in your scenario.

    Since it seems the default is ASCII, you actually don't 'need' to change\specify any Encoding at the moment.

    In regards to the obsolete flag warning, If you're asking how to handle the warning and keep the default encoding, you could use TarOutputStream.TarOutputStream(Stream, null) ctor method signature.


    Update (In reference to Maintainer's comments as well as responses to Github issue)

    The default behavior of entry encoding process when specifying null in TarOutputStream.TarOutputStream(Stream, null) is

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

    In regards to recommendation on specifying encoding:

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

    As such, my recommendation is echoing that advice. You call the non-obsolete constructor and specify 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);
    

    thanks, @piksel bitworks and @Panagiotis Kanavos