我怎么压缩在C#中使用GZipStream多个文件 [英] how do I zip multiple files using GZipStream in c#
问题描述
我已经使用的数据表,并希望所有的压缩文件,这些到一个单一的压缩文件创建多个CSV文件。
这是我做的一切动态。
我试过以下code
列表<串GT; FILESTREAM = GenerateCSVfiles(集);//生成CSV文件
公开名单<串GT; GenerateCSVfiles(列表< DataSet中>数据集)
{
清单<串GT; FILESTREAM =新的List<串GT;();
StringBuilder的结果= NULL; 的foreach(在数据集数据集数据集)
{
结果=新的StringBuilder(); System.Data.DataTable的dataTable = dataSet.Tables [0]; 的foreach(在dataTable.Columns的DataColumn科尔姆)
{
result.Append(colm.ColumnName +,);
} result.Append(\\ n); //创建csv文件
的foreach(在dataTable.Rows的DataRow行)
{
的for(int i = 0; I< dataTable.Columns.Count;我++)
{
result.Append(行[I]的ToString());
result.Append(我== dataTable.Columns.Count - 1\\ n:,?);
}
} filestream.Add(result.ToString());
} 返回文件流;
}
现在在列表< FILESTREAM>
我拥有所有2文件数据要创建2个不同的的.csv
文件和动态创建我怎么能这样做有一个 temp.gz
文件?
在的ActionResult
方法我试过以下code生成.ZIP文件,但它已损坏
返回文件(比较pressStringToFile(temp.gz,文件流),System.Net.Mime.MediaTypeNames.Application.Zip,temp.gz);
// zip文件生成code
字节公众[]的COM pressStringToFile(字符串文件名,目录<串GT; FILESTREAM)
{
MemoryStream的F2 =新的MemoryStream();
的FileStream FS =新的FileStream();
GZipStream GZ =新GZipStream(F2,COM pressionMode.Com preSS,FALSE); 的foreach(在FILESTREAM串OSTR)
{
字节[] B = GetBytes会(OSTR);
gz.Write(B,0,b.length个);
} 返回f2.ToArray();
}
纠正我,生成.zip文件。 (里面2的.csv文件)
块引用>解决方案您不能用
GZipStream
做到这一点。你需要一个第三方库。也许是最好的一出有 SharpZipLib 。我用这个在过去的项目,它是非常容易使用。再加上它已经存在了一段时间等都应该pretty稳定,无缺陷的。I have created multiple csv files using datatable and want to zip all those file into one single zip file. Which is i'm doing all at dynamically.
I tried following code
List<string> filestream = GenerateCSVfiles(dataSets); //Generate CSV files public List<string> GenerateCSVfiles(List<DataSet> dataSets) { List<string> filestream = new List<string>(); StringBuilder result = null; foreach (DataSet dataSet in dataSets) { result = new StringBuilder(); System.Data.DataTable dataTable = dataSet.Tables[0]; foreach (DataColumn colm in dataTable.Columns) { result.Append(colm.ColumnName+","); } result.Append("\n"); //create csv file foreach (DataRow row in dataTable.Rows) { for (int i = 0; i < dataTable.Columns.Count; i++) { result.Append(row[i].ToString()); result.Append(i == dataTable.Columns.Count - 1 ? "\n" : ","); } } filestream.Add(result.ToString()); } return filestream; }
Now in
List<filestream>
I have all 2 file data want to create 2 different.csv
files and create onetemp.gz
file dynamically How can I do this ?On
ActionResult
method I tried following code to generate .zip file but it is corruptedreturn File(CompressStringToFile("temp.gz", filestream), System.Net.Mime.MediaTypeNames.Application.Zip, "temp.gz");
// zip file generation code
public byte[] CompressStringToFile(string fileName, List<string> filestream) { MemoryStream f2 = new MemoryStream(); FileStream fs = new FileStream(); GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false); foreach (string oStr in filestream) { byte[] b = GetBytes(oStr); gz.Write(b, 0, b.Length); } return f2.ToArray(); }
Correct me to generate .zip file. (with inside 2 .csv files)
解决方案You can't do this with a
GZipStream
. You'll need a 3rd party library. Probably the best one out there is SharpZipLib. I've used this on projects in the past, and it's very easy to use. Plus it's been around for a while so should be pretty stable and bug-free.这篇关于我怎么压缩在C#中使用GZipStream多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!