我将对象序列化为JSON。我想在ZIP中压缩它。 ZIP文件已创建,但结果显示已损坏且为空。如果你能说出来,我将非常感激。
public void Write(List<Dictionary<string,string>> data)
{
if (data.Count == 0) // If the collection is empty, then return
return;
PartNumber++; // In the archive are added many files. This is part of a single file
string FullFileName = FolderWrite + FileName + ".zip";
fsWrite = new FileStream(FullFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
ZipArchive archive = new ZipArchive(fsWrite, ZipArchiveMode.Update);
ZipArchiveEntry entry = archive.CreateEntry(FileName + ".part_" + PartNumber.ToString(), CompressionLevel.Optimal);
Stream s = entry.Open();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Dictionary<string,string>>));
ser.WriteObject(s, data);
s.Close();
fsWrite.Close();
}
答案 0 :(得分:0)
需要
archive.Dispose();
结果
public void Write(List<Dictionary<string,string>> data)
{
if (data.Count == 0)
return;
PartNumber++;
string FullFileName = FolderWrite + FileName + ".zip";
ZipArchive archive;
fsWrite = new FileStream(FullFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
archive = new ZipArchive(fsWrite, ZipArchiveMode.Update);
ZipArchiveEntry entry = archive.CreateEntry(FileName + ".part_" + PartNumber.ToString(), CompressionLevel.Optimal);
Stream s = entry.Open();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Dictionary<string,string>>));
ser.WriteObject(s, data);
archive.Dispose();
s.Close();
fsWrite.Close();
}
}