本文整理汇总了C#中ZipArchive.Close方法的典型用法代码示例。如果您正苦于以下问题:C# ZipArchive.Close方法的具体用法?C# ZipArchive.Close怎么用?C# ZipArchive.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZipArchive
的用法示例。
在下文中一共展示了ZipArchive.Close方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteDataToZipFile
public int WriteDataToZipFile(string zipPath, CancellationToken cancellationToken, Action<string, double> progressCallback)
{
// create a zip file to hold all data
#if __ANDROID__
ZipOutputStream zipFile = null;
#elif __IOS__
ZipArchive zipFile = null;
#endif
// write all data to separate JSON files. zip files for convenience.
string directory = null;
Dictionary<string, StreamWriter> datumTypeFile = new Dictionary<string, StreamWriter>();
try
{
string directoryName = Protocol.Name + "_Data_" + DateTime.UtcNow.ToShortDateString() + "_" + DateTime.UtcNow.ToShortTimeString();
directoryName = new Regex("[^a-zA-Z0-9]").Replace(directoryName, "_");
directory = Path.Combine(SensusServiceHelper.SHARE_DIRECTORY, directoryName);
if (Directory.Exists(directory))
Directory.Delete(directory, true);
Directory.CreateDirectory(directory);
if (progressCallback != null)
progressCallback("Gathering data...", 0);
int totalDataCount = 0;
foreach (Tuple<string, string> datumTypeLine in GetDataLinesToWrite(cancellationToken, progressCallback))
{
string datumType = datumTypeLine.Item1;
string line = datumTypeLine.Item2;
StreamWriter file;
if (datumTypeFile.TryGetValue(datumType, out file))
file.WriteLine(",");
else
{
file = new StreamWriter(Path.Combine(directory, datumType + ".json"));
file.WriteLine("[");
datumTypeFile.Add(datumType, file);
}
file.Write(line);
++totalDataCount;
}
// close all files
foreach (StreamWriter file in datumTypeFile.Values)
{
file.Write(Environment.NewLine + "]");
file.Close();
}
cancellationToken.ThrowIfCancellationRequested();
if (progressCallback != null)
progressCallback("Compressing data...", 0);
#if __ANDROID__
directoryName += '/';
zipFile = new ZipOutputStream(new FileStream(zipPath, FileMode.Create, FileAccess.Write));
zipFile.PutNextEntry(new ZipEntry(directoryName));
int dataWritten = 0;
foreach (string path in Directory.GetFiles(directory))
{
// start json file for data of current type
zipFile.PutNextEntry(new ZipEntry(directoryName + Path.GetFileName(path)));
using (StreamReader file = new StreamReader(path))
{
string line;
while ((line = file.ReadLine()) != null)
{
if (progressCallback != null && totalDataCount >= 10 && (dataWritten % (totalDataCount / 10)) == 0)
progressCallback(null, dataWritten / (double)totalDataCount);
cancellationToken.ThrowIfCancellationRequested();
zipFile.Write(file.CurrentEncoding.GetBytes(line + Environment.NewLine));
if (line != "[" && line != "]")
++dataWritten;
}
}
zipFile.CloseEntry();
System.IO.File.Delete(path);
}
// close entry for directory
zipFile.CloseEntry();
#elif __IOS__
zipFile = new ZipArchive();
zipFile.CreateZipFile(zipPath);
//.........这里部分代码省略.........