[英]Writing a text file in memory without storing to disk
I have a list of string as input, which I need to put into a text file, and then put that text file into an encrypted ZIP archive. 我有一个字符串列表作为输入,我需要将其放入一个文本文件,然后将该文本文件放入加密的ZIP存档。 I want to do this in two steps:
我想分两步完成:
However, there is one main requirement: For security reasons I am not allowed to write the text file to disk ! 但是,有一个主要要求:出于安全考虑,我不允许将文本文件写入磁盘 !
I've been playing around with memory streams and stream writers etc. but can't seem to figure it out. 我一直在玩内存流和流编写器等,但似乎无法搞清楚。 I'd prefer UTF8 encoding.
我更喜欢UTF8编码。
private byte[] GenerateTextFile(List<string> contents)
{
// Generate a text file in memory and return its bytes
}
Can you guys help me out? 你能帮助我吗?
That's an easy one, you should concat all the strings into one and then call Encoding.UTF8.GetBytes()
这很简单,你应该将所有字符串连接成一个,然后调用
Encoding.UTF8.GetBytes()
To concat the strings you can use: 要连接您可以使用的字符串:
string.Join(delimiter, contents)
If you don't need the delimeter just remove it, otherwise you probably need to use a \\n
如果您不需要分隔符,只需删除它,否则您可能需要使用
\\n
So you would have this method: 所以你会有这个方法:
private byte[] GenerateTextFile(List<string> contents)
{
return Encoding.UTF8.GetBytes(string.Join(delimiter, contents));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.