adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.25k stars 479 forks source link

Incorrect encoding when filename had more than 100 characters #145

Open alexandrejh opened 8 years ago

alexandrejh commented 8 years ago

When the filename had more than 100 characters and diacritics the folder name is created with wrong encode and the filename is cutted (e.g. if the name is myword.docx in tar.gz file the name is myword.do)

adamhathcock commented 8 years ago

Tar has a size limitation on names. I guess an exception should be thrown instead of truncating. Thoughts?

Any chance of a pull request fix?

alexandrejh commented 8 years ago

At this moment i used a work around in my code (remove diacritics from the path) but this is not good because this name is typed by user. In TarHeader has the code above that works differently when the filename has more than 100 characters.

//Encoding.UTF8.GetBytes("magic").CopyTo(buffer, 257); if (Name.Length > 100) { // Set mock filename and filetype to indicate the next block is the actual name of the file WriteStringBytes("././@LongLink", buffer, 0, 100); buffer[156] = (byte) EntryType.LongName; WriteOctalBytes(Name.Length + 1, buffer, 124, 12); } else { WriteStringBytes(Name, buffer, 0, 100); WriteOctalBytes(Size, buffer, 124, 12); var time = (long) (LastModifiedTime.ToUniversalTime() - Epoch).TotalSeconds; WriteOctalBytes(time, buffer, 136, 12); buffer[156] = (byte) EntryType;

            if (Size >= 0x1FFFFFFFF)
            {
                byte[] bytes = DataConverter.BigEndian.GetBytes(Size);
                var bytes12 = new byte[12];
                bytes.CopyTo(bytes12, 12 - bytes.Length);
                bytes12[0] |= 0x80;
                bytes12.CopyTo(buffer, 124);
            }
        }
adamhathcock commented 8 years ago

Sounds like you have a good handle on the issue. I'm not a tar expert but it might be cause tar only expects to deal with ASCII characters? I don't really know.

Could you make a PR that could fix this for everyone?

alexandrejh commented 8 years ago

yes, i can do the PR, but remove all folder name diacritics is interesting? if yes i can do.

alexandrejh commented 8 years ago

when i try to open the project in my VS 2015 i got this error

D:\VisualStudio\SharpCompress\sharpcompress\src\SharpCompress\SharpCompress.xproj : error : The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk. D:\VisualStudio\SharpCompress\sharpcompress\src\SharpCompress\SharpCompress.xproj

D:\VisualStudio\SharpCompress\sharpcompress\test\SharpCompress.Test\SharpCompress.Test.xproj : error : The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk. D:\VisualStudio\SharpCompress\sharpcompress\test\SharpCompress.Test\SharpCompress.Test.xproj

adamhathcock commented 8 years ago

You need the .NET Core Preview 2 tooling that uses xproj. I haven't quite updated to that yet but I will after I return from vacation.

adamhathcock commented 8 years ago

Right now the code is RC2 but I need to make it RTM for .NET Core and .NET Standard

adamhathcock commented 7 years ago

You can fix this now that everything is RTM.

http://dot.net