使用ICSharpCode.SharpZLib,讀取Tar.gz壓縮包的檔案名稱亂碼問題

來源:互聯網
上載者:User
預設版本的庫檔案讀取時會將tar包中的中文路徑讀成亂碼,兩種方案, 第一種方法,不改SharpZLib的原始碼,將讀出的Entry名稱進行如下轉換:private
string ParseName(string source){byte[] sourceByte =
new byte[source.Length];for (int i = 0; i < source.Length; i++){sourceByte[i] = (byte)source[i];}return
Encoding.Default.GetString(sourceByte);

}  即可將其轉化為正常,註:SharpZLib中,是將byte[]數組中的每一個元素強制轉化為char,然後再拼成string,所以會出現亂碼,這裡的處理是將其恢複原來的byte[]數組,再使用gb2312的編碼解出來,得到中文   第二種方法,修改SharpZLib的原始碼(有風險,未詳細測試)將TarHeader.cs中的ParseName函數改為下文所示,可解決這一問題: static
public String ParseName(byte[] header,
int offset, int length)        {            if ( header ==
null ) {                throw
new ArgumentNullException("header");            }            if ( offset < 0 ) {#if NETCF_1_0                throw new ArgumentOutOfRangeException("offset");#else                throw
new ArgumentOutOfRangeException("offset",
"Cannot be less than zero");#endif                            }            if ( length < 0 )            {#if NETCF_1_0                throw new ArgumentOutOfRangeException("length");#else                throw
new ArgumentOutOfRangeException("length",
"Cannot be less than zero");#endif                            }            if ( offset + length > header.Length )            {                throw
new ArgumentException("Exceeds header size",
"length");            }//StringBuilder result = new StringBuilder(length);            //for (int i = offset; i < offset + length; ++i) {// if (header[i] == 0) {// break;// }// result.Append((char)header[i]);//}            //return result;int tmpCount = 0;for (int i = offset; i < offset + length; ++i){if (header[i] == 0){break;}else tmpCount++;}byte[] bytes =
new byte[tmpCount];for (int i = offset; i < offset+tmpCount; i++){bytes[i] = header[i]; }string res = System.Text.Encoding.Default.GetString(bytes);return res;

        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.