タイトルはそれをすべて言います:
- 私は tar.gz アーカイブをそのように読みました
- ファイルをバイト配列に分割する
- これらのバイトを Base64 文字列に変換します
- そのBase64文字列をバイト配列に変換します
- これらのバイトを新しい tar.gz ファイルに書き戻します
両方のファイルが同じサイズであることは確認できますが (以下のメソッドは true を返します)、コピー バージョンを抽出できなくなりました。
何か不足していますか?
Boolean MyMethod(){
using (StreamReader sr = new StreamReader("C:\...\file.tar.gz")) {
String AsString = sr.ReadToEnd();
byte[] AsBytes = new byte[AsString.Length];
Buffer.BlockCopy(AsString.ToCharArray(), 0, AsBytes, 0, AsBytes.Length);
String AsBase64String = Convert.ToBase64String(AsBytes);
byte[] tempBytes = Convert.FromBase64String(AsBase64String);
File.WriteAllBytes(@"C:\...\file_copy.tar.gz", tempBytes);
}
FileInfo orig = new FileInfo("C:\...\file.tar.gz");
FileInfo copy = new FileInfo("C:\...\file_copy.tar.gz");
// Confirm that both original and copy file have the same number of bytes
return (orig.Length) == (copy.Length);
}
編集: 作業例ははるかに単純です (@TS のおかげで):
Boolean MyMethod(){
byte[] AsBytes = File.ReadAllBytes(@"C:\...\file.tar.gz");
String AsBase64String = Convert.ToBase64String(AsBytes);
byte[] tempBytes = Convert.FromBase64String(AsBase64String);
File.WriteAllBytes(@"C:\...\file_copy.tar.gz", tempBytes);
FileInfo orig = new FileInfo(@"C:\...\file.tar.gz");
FileInfo copy = new FileInfo(@"C:\...\file_copy.tar.gz");
// Confirm that both original and copy file have the same number of bytes
return (orig.Length) == (copy.Length);
}
ありがとう!