本文整理汇总了C#中ZipArchive.CreateZipFile方法的典型用法代码示例。如果您正苦于以下问题:C# ZipArchive.CreateZipFile方法的具体用法?C# ZipArchive.CreateZipFile怎么用?C# ZipArchive.CreateZipFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZipArchive
的用法示例。
在下文中一共展示了ZipArchive.CreateZipFile方法的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
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);
//.........这里部分代码省略.........