将文件转换为Base64字符串,再将其转回文件

169

标题已经说明了一切:

  1. 我这样从一个tar.gz归档文件中读取:
  2. 将文件分成字节数组
  3. 将这些字节转换为Base64字符串
  4. 将该Base64字符串转换回字节数组
  5. 将这些字节写回新的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);
}

编辑:感谢@T.S.,工作示例要简单得多:

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);
}

感谢!

你不能这样随意更改压缩文件的内容。你需要在第一步中先解压文件,而不是直接按原样读取它。然后第五步也必须重新压缩数据,而不是直接写出字节。 - itsme86
1
幸运的是,由于文件本身没有实际操作(基本上只是从A点移动到B点),因此这个特定任务不需要任何(解压/压缩)处理。 - darkpbj
4个回答

420

如果你因为某种原因想将文件转换为base-64字符串,例如想通过网络传输等等,你可以这样做

Byte[] bytes = File.ReadAllBytes("path");
String file = Convert.ToBase64String(bytes);

相应地,将其从文件中读回:

Byte[] bytes = Convert.FromBase64String(b64Str);
File.WriteAllBytes(path, bytes);

1
谢谢提供的信息,我尝试按照下面的答案操作,但没有帮助,不过使用一个简单的openFileDialog似乎解决了我的问题。 - Mister SirCode
如果ToBase64String返回System.OutOfMemoryException怎么办?如何针对大文件和有限内存进行优化? - Olorunfemi Davis
@OlorunfemiAjibulu,我猜你需要使用流或将字符串分成几部分。有一次,我为大文件编写了自定义加密程序,我们保存了加密块。我们添加了4个字节来保存块大小的整数值。这样我们就知道要读取多少位置了。 - T.S.
有趣的@TaylorSpark。我使用了流,很好用。 - Olorunfemi Davis

0

另一个在VB.NET中工作的示例:

Public Function base64Encode(ByVal myDataToEncode As String) As String
    Try
        Dim myEncodeData_byte As Byte() = New Byte(myDataToEncode.Length - 1) {}
        myEncodeData_byte = System.Text.Encoding.UTF8.GetBytes(myDataToEncode)
        Dim myEncodedData As String = Convert.ToBase64String(myEncodeData_byte)
        Return myEncodedData
    Catch ex As Exception
        Throw (New Exception("Error in base64Encode" & ex.Message))
    End Try
    '
End Function

-1
private String encodeFileToBase64Binary(File file){    
String encodedfile = null;  
try {  
    FileInputStream fileInputStreamReader = new FileInputStream(file);  
    byte[] bytes = new byte[(int)file.length()];
    fileInputStreamReader.read(bytes);  
    encodedfile = Base64.encodeBase64(bytes).toString();  
} catch (FileNotFoundException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
} catch (IOException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}  
    return encodedfile;  
}

3
虽然这段代码可能回答了问题,但提供有关它如何以及/或者为什么解决问题的附加上下文将会提高答案的长期价值。请阅读此如何回答页面,以提供优质的答案。 - thewaywewere
4
如果OP使用了C#,为什么还需要Java? - T.S.
4
为什么我们需要 Java? - Click Ok

-4

对于Java,考虑使用Apache Commons FileUtils

/**
 * Convert a file to base64 string representation
 */
public String fileToBase64(File file) throws IOException {
    final byte[] bytes = FileUtils.readFileToByteArray(file);
    return Base64.getEncoder().encodeToString(bytes);
}

/**
 * Convert base64 string representation to a file
 */
public void base64ToFile(String base64String, String filePath) throws IOException {
    byte[] bytes = Base64.getDecoder().decode(base64String);
    FileUtils.writeByteArrayToFile(new File(filePath), bytes);
}

4
这个问题标记为C#,如果您能提供C#的解决方案会更好。 - Ishaan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接