【发布时间】:2012-03-31 08:28:41
【问题描述】:
我有 5 个 MemoryStream。我想创建一个新的 zip(这也将是一个 Stream),而我拥有的 5 个 MemoryStream 中的每一个都将代表一个文件。
我确实有这段关于如何压缩字符串/1 MemoryStream 的代码。
public static string Zip(string value)
{
//Transform string into byte[]
byte[] byteArray = new byte[value.Length];
int indexBA = 0;
foreach (char item in value.ToCharArray())
{
byteArray[indexBA++] = (byte)item;
}
//Prepare for compress
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
System.IO.Compression.CompressionMode.Compress);
//Compress
sw.Write(byteArray, 0, byteArray.Length);
//Close, DO NOT FLUSH cause bytes will go missing...
sw.Close();
//Transform byte[] zip data to string
byteArray = ms.ToArray();
System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
foreach (byte item in byteArray)
{
sB.Append((char)item);
}
ms.Close();
sw.Dispose();
ms.Dispose();
return sB.ToString();
}
这段代码很好,但我需要在 MemoryStreams 之间进行某种分离。我不希望它们是连续的。 (最好我希望它们位于 ZipStream 中的不同文件上)
如何在 ZipStream 中创建文件(或类似的分隔符)?
【问题讨论】:
标签: c# .net file memorystream gzipstream