简体   繁体   English

在内存中写入文本文件而不存储到磁盘

[英]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: 我想分两步完成:

  1. Create a text file and get its bytes 创建一个文本文件并获取其字节
  2. Write the bytes into the archive using a zip library 使用zip库将字节写入存档

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.

相关问题 使用GZipStream将文本文件写入gz文件,而无需先将文本文件写入磁盘 - Writing a text file into a gz file using GZipStream without first writing the text file to disk 将大文件写入磁盘内存不足异常 - Writing Large File To Disk Out Of Memory Exception 如何压缩文件并通过HTTP发送而无需写入磁盘或将完整文件加载到内存中? - How to zip a file and send over HTTP without writing to disk or loading the full file in memory? 通过我的服务器将文件从远程服务器复制到客户端浏览器,而无需将整个文件写入磁盘或内存 - Copy file from remote server to client browser via my server without writing the entire file to disk or memory 从 memory 中的.zip 文件中获取字节数组,无需向磁盘写入任何内容 - Get byte array from .zip file in memory, without writing anything to disk 将byte []转换为文件流而无需写入磁盘 - Convert byte[] to file stream without writing to disk WebAPI 文件上传 - 无需将文件写入磁盘 - WebAPI File Uploading - Without writing files to disk 使用流创建文本文件,但不写入磁盘 - Creating text file using stream but not writing to disk C#Asp.Net创建文本文件,压缩并保存到Blob - 无需向磁盘写入任何内容 - C# Asp.Net Create text file, zip it, and save to Blob - Without writing anything to disk 内存中视频的屏幕截图,无需将视频或屏幕截图写入磁盘 - Screenshot from video in memory without writing video or screenshot to disk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM