1
我想實現SharpZipLibSharpZipLib - 零個字節
所以我複製了他們的樣品下面的代碼片段:
// Compresses the supplied memory stream, naming it as zipEntryName, into a zip,
// which is returned as a memory stream or a byte array.
//
public MemoryStream CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName)
{
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
ZipEntry newEntry = new ZipEntry(zipEntryName);
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
zipStream.CloseEntry();
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
outputMemStream.Position = 0;
return outputMemStream;
// Alternative outputs:
// ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
byte[] byteArrayOut = outputMemStream.ToArray();
// GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
byte[] byteArrayOut = outputMemStream.GetBuffer();
long len = outputMemStream.Length;
}
我複製粘貼該函數並把它稱爲是這樣的:
using (MemoryStream ms = new MemoryStream())
using (FileStream file = new FileStream(@"c:\file.jpg", FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms.Write(bytes, 0, (int)file.Length);
var result = SharpZip.CreateToMemoryStream(ms, "file.jpg");
result.WriteTo(new FileStream(@"c:\myzip.zip", FileMode.Create, System.IO.FileAccess.Write));
}
myzip.zip成功創建,但file.jpg裏面有零字節。
任何想法?
非常感謝
你是否已經通過代碼進行了寫作/創建新FileStream的結果是什麼? – MethodMan 2014-09-03 15:23:01
是的,它是一個有效的內存流。 ** myzip.zip **被創建,** file.jpg **也被創建,但圖像的零字節。 – 2014-09-03 15:25:24
不關閉zipstream關閉底層內存流?這將解釋爲什麼你沒有數據。 – Sign 2014-09-03 15:27:28