将文件转换为Base64String并再次返回

时间:2014-09-18 17:59:41

标签: c# base64 streamreader system.io.fileinfo

标题说明了一切:

  1. 我在这样的tar.gz档案中读到了
  2. 将文件分成字节数组
  3. 将这些字节转换为Base64字符串
  4. 将该Base64字符串转换回字节数组
  5. 将这些字节写回新的tar.gz文件
  6. 我可以确认两个文件大小相同(以下方法返回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);
    }
    

    谢谢!

4 个答案:

答案 0 :(得分:208)

如果您因某种原因希望将文件转换为base-64字符串。就像你想通过互联网传递它一样......你可以这样做

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

相应地,请回读文件:

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

答案 1 :(得分:3)

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

答案 2 :(得分:1)

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

答案 3 :(得分: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