我有一个简单的http server人上传文件,我只是压缩它们并将它们保存到磁盘。这是我在做什么,我认为人们会理解的代码更好修改C#中的流#
/// <summary>
/// Client uploads files through here
/// </summary>
private static void Server_ProcessRequest(HttpListenerContext context)
{
// I know that context.Request.InputStream starts like: ------WebKitFormBoundaryePkpFF7tjBAqx29L
// and it also ends with that boundary
// 1. I read from context.Request.InputStream and skip the headers
// (I read until I find 4 new lines)
// 2. I then write the file to disk
// var sizeOfFile = context.Request.ContentLength64;
// etc... read from stream
// 3. create a new stream in order to compress file later
var newStream = System.IO.File.Open("FileJustCreated");
// 4. MySealedLibraryThatICannotModify.CompressFile(newStream,"CompressedFileLocation.zip");
}
所以我在这里的问题是如何避免该文件写入磁盘?我不能将context.Request.InputStream
传递给压缩文件的方法,因为它包含头文件和其他不属于该文件的东西。如果我可以创建一个返回新流的方法,那将会很棒。 这些文件可能很大,我不想将所有内容都放在内存中,因此我期待创建一个像System.IO.File.OpenRead
这样的方法,它将返回一个流,其中只有您读取的部分位于内存中。
我知道我可以使用不同的库来压缩文件,但是如果我坚持这个库并且学习新的东西,它会很好。
这里典型的做法是与跟踪占用的字节数,并拒绝出具过去的这个数据的另一个(自定义)流来包装一个流点。在包装它之前,您通常会将原始流消耗到所需的点。我可以挖掘一下我从古代历史中得到的实现,如果它有帮助的话......(我曾经在protobuf-net中做过这件事) –
phew,采取了一些挖掘,但是:'SubStream.cs':https: //github.com/mgravell/protobuf-net/blob/fd39e2f464273cfe942ab5ab793a7087ae881220/protobuf-net/SubStream.cs –
为什么不能简单地将部分读取流传递给'CompressFile'?在阅读之前它是否真的重置了流?我认为你可以跳到你想要开始使用原始流并将其传入的位置,假设它以合理的方式读取它。 – Servy