本文整理汇总了C#中ZipArchive.CloseZipFile方法的典型用法代码示例。如果您正苦于以下问题:C# ZipArchive.CloseZipFile方法的具体用法?C# ZipArchive.CloseZipFile怎么用?C# ZipArchive.CloseZipFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZipArchive
的用法示例。
在下文中一共展示了ZipArchive.CloseZipFile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Zip
// This is the Zip Implementation Method
partial void Zip(NSObject sender)
{
// Get the current working directory and set the desired name of the Zip File
string path = Environment.CurrentDirectory;
string fileName = "/file.zip";
theZipFile = path + fileName;
// Create a new instance of ZipArchive
ZipArchive zip = new ZipArchive();
// You can subscribe to OnError event so you can handle
// any errors situations
zip.OnError += (object s, EventArgs e) =>
{
string error = s as String;
Console.WriteLine("Error:" + error);
};
// Create the Zip file on the desired path
zip.CreateZipFile(theZipFile);
// Add the files you want zip, this will return "true" if
// the file was successfully added to zip file
bool added = zip.AddFile(path + "/xamarin.jpg", "xamarin.jpg");
// Dont forget to close the zip file
zip.CloseZipFile();
if(added)
new UIAlertView("Info", "Success: " + theZipFile, null, "Great", null).Show();
else
new UIAlertView("Info", "Something went wrong", null, "Ok", null).Show();
Console.WriteLine(theZipFile);
}
示例2: WriteDataToZipFile
//.........这里部分代码省略.........
// 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);
zipFile.AddFolder(directory, null);
#endif
if (progressCallback != null)
progressCallback(null, 1);
return totalDataCount;
}
finally
{
// ensure that zip file is closed.
try
{
if (zipFile != null)
{
#if __ANDROID__
zipFile.Close();
#elif __IOS__
zipFile.CloseZipFile();
#endif
}
}
catch (Exception)
{
}
// ensure that all temporary files are closed/deleted.
foreach (string datumType in datumTypeFile.Keys)
{
try
{
datumTypeFile[datumType].Close();
}
catch (Exception)
{
}
}
try
{
Directory.Delete(directory, true);
}
catch (Exception)
{
}
}
}