[英]Reading Multiple files in a Byte Array
I have an object that has a byte array property 'ZipFile' to store a file stream: 我有一个对象,它有一个字节数组属性'ZipFile'来存储文件流:
Property in Result class: Result类中的属性:
public class Result
{
public byte[] ZipFile;
}
In my application, I generate a PDF file, and read the file into the 'ZipFile' property using my ReadFile method like this : 在我的应用程序中,我生成一个PDF文件,并使用我的ReadFile方法将文件读入'ZipFile'属性,如下所示:
objResult.ZipFile = ReadFile(FilePath);
Signature of ReadFile method: ReadFile方法的签名:
private byte[] ReadFile(string strFileName)
The Problem: 问题:
My ReadFile method will now be called in a loop, because I am generating multiple PDF files. 我的ReadFile方法现在将在循环中调用,因为我正在生成多个PDF文件。 Each time the ReadFile method will read a new file from the specifed parameter to the 'objResult.ZipFile' property, consequently replacing the old value in 'ZipFile' property.
每次ReadFile方法都会将指定参数中的新文件读取到'objResult.ZipFile'属性,从而替换'ZipFile'属性中的旧值。 Now, I want my 'ZipFile' property to store multiple PDF files stream.
现在,我希望我的'ZipFile'属性存储多个PDF文件流。 So what should I do for that?
那我该怎么做呢? Should I just change this property to a two dimensional byte[][] array, or is there any better way to do this?
我应该将此属性更改为二维byte [] []数组,还是有更好的方法来执行此操作? Remember, that this property will be used for saving(writing) these files by calling method.
请记住,此属性将用于通过调用方法保存(写入)这些文件。 Open to all Suggestions.
对所有建议开放。 Thanks.
谢谢。
Sounds like you should either have a List<Result>
, or result should have a collection such as List<byte[]>
as a ZipFiles
property. 听起来像您应该有一个
List<Result>
或结果应具有收集诸如List<byte[]>
作为ZipFiles
属性。 Note that currently you don't have a property at all - you have a public field, which is generally a bad idea. 请注意,目前您根本没有属性 - 您有一个公共字段,这通常是一个坏主意。
(You probably wouldn't expose it as a List<byte[]>
- that would be the underlying implementation. I'd probably make it an IEnumerable<byte[]>
and expose an AddZipFile
method.) (您可能不会将其公开为
List<byte[]>
- 这将是底层实现。我可能会将其设置为IEnumerable<byte[]>
并公开AddZipFile
方法。)
You may use List<byte[]>
(list of byte array) and then append the array in the list in each iteration. 您可以使用
List<byte[]>
(字节数组列表),然后在每次迭代中将数组附加到列表中。 Something like. 就像是。
public class Result
{
public List<byte[]> ZipFilesList { get; set; };
}
Later you can do: 以后你可以这样做:
ResultObj.ZipFilesList.Add(ReadFile(FilePath));
You need to have list of List<byte[]>
您需要列出
List<byte[]>
public class Result
{
public List<byte[]> ZipFiles;
}
And add the files into your list 并将文件添加到列表中
objResult.ZipFiles.Add(ReadFile(FilePath));
If you just want to store them for later processing a list or queue will do it. 如果您只想存储它们以供以后处理,则列表或队列将执行此操作。 I think I would opt for a queue as your usage pattern seems to match that.
我想我会选择一个队列,因为你的使用模式似乎与之匹配。
// make queue
var filesQueue = new Queue<byte[]>();
// add file
filesQueue.Enqueue(newFile);
// get file
var fileToSave=filesQueue.Dequeue();
You can switch to a concurrentQueue later if you would ever need to parallelize etc.. 如果您需要并行化等,可以稍后切换到concurrentQueue。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.