DotNetZip中有一個使用Stream的方法,其方法爲AddEntry
。
String zipToCreate = "Content.zip";
String fileNameInArchive = "Content-From-Stream.bin";
using (System.IO.Stream streamToRead = MyStreamOpener())
{
using (ZipFile zip = new ZipFile())
{
ZipEntry entry= zip.AddEntry(fileNameInArchive, streamToRead);
zip.Save(zipToCreate); // the stream is read implicitly here
}
}
使用LinqPad一個小測試表明,它可以使用一個MemoryStream來構建zip文件
void Main()
{
UnicodeEncoding uniEncoding = new UnicodeEncoding();
byte[] firstString = uniEncoding.GetBytes("This is the current contents of your TextBox");
using(MemoryStream memStream = new MemoryStream(100))
{
memStream.Write(firstString, 0 , firstString.Length);
// Reposition the stream at the beginning (otherwise an empty file will be created in the zip archive
memStream.Seek(0, SeekOrigin.Begin);
using (ZipFile zip = new ZipFile())
{
ZipEntry entry= zip.AddEntry("TextBoxData.txt", memStream);
zip.Save(@"D:\temp\memzip.zip");
}
}
}
請注意:ZIP格式並不打算用作虛擬存儲,因此幾乎所有修改都會導致大部分歸檔文件被重寫。 –
你絕對是對的@ EugeneMayevski'EldoSCorp如果這是真正的意圖,那麼一個zip文件並不是專門用於大量數據的可行解決方案。 – Steve