use*_*444 2 .net c# memorystream file gzipstream
我有5个MemoryStreams.我想创建一个新的zip(也将是一个Stream),而我拥有的5个MemoryStream中的每一个都会重新发送一个文件.
我确实有关于如何Zip一个字符串/ 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();
}
Run Code Online (Sandbox Code Playgroud)
这段代码很好,但我需要在MemoryStream之间进行某种分离.我不希望他们连续.(最好我希望它们在ZipStream中的不同文件中)
如何在ZipStream中创建文件(或类似的分隔符)?
.net库中的GZipStream并不适合创建zip文件.它的主要目的是压缩可能用于发送数据的流,或者在上面的示例中压缩流到文件.
写入扩展名为.gz的文件的压缩GZipStream对象可以使用许多常用的压缩工具进行解压缩.但是,此类本身并不提供将文件添加到.zip存档或从.zip存档中提取文件的功能.
你可以让GZip库创建一个包含多个压缩文件的zip文件,但是你需要一些腿部工作.有一个博客张贴在这里将告诉您如何做到这一点.
但是,您可以考虑其他一些解决方案:
| 归档时间: |
|
| 查看次数: |
3799 次 |
| 最近记录: |