本文整理汇总了C#中ZipArchive类的典型用法代码示例。如果您正苦于以下问题:C# ZipArchive类的具体用法?C# ZipArchive怎么用?C# ZipArchive使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ZipArchive类属于命名空间,在下文中一共展示了ZipArchive类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnZip
partial void UnZip(NSObject sender)
{
// Set the desired output directory to place unzipped files
string theUnzippedFolder = Environment.CurrentDirectory + "/unzipped";
// Create a new instance of ZipArchiv
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);
};
// Open the zip file you want to unzip
zip.UnzipOpenFile(theZipFile);
// This will return true if succeeded to unzip
bool unzipped = zip.UnzipFileTo(theUnzippedFolder, true);
// Dont forget to close the zip file
zip.UnzipCloseFile();
if(unzipped)
new UIAlertView("Info", "Success: " + theUnzippedFolder, null, "Great", null).Show();
else
new UIAlertView("Info", "Something went wrong", null, "Ok", null).Show();
Console.WriteLine(theUnzippedFolder);
}
示例2: UnZip
private async static Task<List<StorageFile>> UnZip()
{
var files = new List();
// open zip
var zipFile = await Package.Current.InstalledLocation.GetFileAsync("Data.zip");
using (var zipStream = await zipFile.OpenReadAsync())
{
using (var archive = new ZipArchive(zipStream.AsStream()))
{
// iterate through zipped objects
foreach (var archiveEntry in archive.Entries)
{
using (var outStream = archiveEntry.Open())
{
// unzip file to app's LocalFolder
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(archiveEntry.Name);
using (var inStream = await file.OpenStreamForWriteAsync())
{
await outStream.CopyToAsync(inStream);
}
files.Add(file);
}
}
}
}
}
示例3: TestCompressedZip
public void TestCompressedZip()
{
var Zip = new ZipArchive();
Zip.Load("../../../TestInput/UncompressedZip.zip");
var ExpectedString = "ffmpeg -i test.pmf -vcodec copy -an test.h264\nffmpeg -i test.pmf -acodec copy -vn test.ac3p";
var ResultString = Zip["demux.bat"].OpenUncompressedStream().ReadAllContentsAsString(FromStart:false);
Assert.AreEqual(ExpectedString, ResultString);
}
示例4: ZipWritableArchiveEntry
internal ZipWritableArchiveEntry(ZipArchive archive, Stream stream, string path, long size, DateTime? lastModified)
: base(archive, null)
{
this.Stream = stream;
this.path = path;
this.size = size;
this.lastModified = lastModified;
}
示例5: TestZip2
public void TestZip2()
{
var Zip = new ZipArchive();
Zip.Load(ResourceArchive.GetFlash0ZipFileStream());
foreach (var Entry in Zip)
{
Console.Error.WriteLine(Entry);
}
}
示例6: TestUncompressedZip
public void TestUncompressedZip()
{
var Zip = new ZipArchive();
Zip.Load("../../../TestInput/UncompressedZip.zip");
foreach (var Entry in Zip)
{
Console.Error.WriteLine(Entry);
}
}
示例7: ZipWritableArchiveEntry
internal ZipWritableArchiveEntry(ZipArchive archive, Stream stream, string path, long size,
DateTime? lastModified, bool closeStream)
: base(archive, null)
{
this.stream = stream;
this.path = path;
this.size = size;
this.LastModifiedTime = lastModified;
this.closeStream = closeStream;
}
示例8: ZipWithInvalidFileNames_ParsedBasedOnSourceOS
public static async Task ZipWithInvalidFileNames_ParsedBasedOnSourceOS(string zipName, string fileName)
{
using (Stream stream = await StreamHelpers.CreateTempCopyStream(compat(zipName)))
using (ZipArchive archive = new ZipArchive(stream))
{
Assert.Equal(1, archive.Entries.Count);
ZipArchiveEntry entry = archive.Entries[0];
Assert.Equal(fileName, entry.Name);
}
}
示例9: EmptyEntryTest
public static void EmptyEntryTest(ZipArchiveMode mode)
{
string data1 = "test data written to file.";
string data2 = "more test data written to file.";
DateTimeOffset lastWrite = new DateTimeOffset(1992, 4, 5, 12, 00, 30, new TimeSpan(-5, 0, 0));
var baseline = new LocalMemoryStream();
using (ZipArchive archive = new ZipArchive(baseline, mode))
{
AddEntry(archive, "data1.txt", data1, lastWrite);
ZipArchiveEntry e = archive.CreateEntry("empty.txt");
e.LastWriteTime = lastWrite;
using (Stream s = e.Open()) { }
AddEntry(archive, "data2.txt", data2, lastWrite);
}
var test = new LocalMemoryStream();
using (ZipArchive archive = new ZipArchive(test, mode))
{
AddEntry(archive, "data1.txt", data1, lastWrite);
ZipArchiveEntry e = archive.CreateEntry("empty.txt");
e.LastWriteTime = lastWrite;
AddEntry(archive, "data2.txt", data2, lastWrite);
}
//compare
Assert.True(ArraysEqual(baseline.ToArray(), test.ToArray()), "Arrays didn't match");
//second test, this time empty file at end
baseline = baseline.Clone();
using (ZipArchive archive = new ZipArchive(baseline, mode))
{
AddEntry(archive, "data1.txt", data1, lastWrite);
ZipArchiveEntry e = archive.CreateEntry("empty.txt");
e.LastWriteTime = lastWrite;
using (Stream s = e.Open()) { }
}
test = test.Clone();
using (ZipArchive archive = new ZipArchive(test, mode))
{
AddEntry(archive, "data1.txt", data1, lastWrite);
ZipArchiveEntry e = archive.CreateEntry("empty.txt");
e.LastWriteTime = lastWrite;
}
//compare
Assert.True(ArraysEqual(baseline.ToArray(), test.ToArray()), "Arrays didn't match after update");
}
示例10: ReadStreamOps
public static async Task ReadStreamOps()
{
using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip")), ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry e in archive.Entries)
{
using (Stream s = e.Open())
{
Assert.True(s.CanRead, "Can read to read archive");
Assert.False(s.CanWrite, "Can't write to read archive");
Assert.False(s.CanSeek, "Can't seek on archive");
Assert.Equal(ZipTest.LengthOfUnseekableStream(s), e.Length); //"Length is not correct on unseekable stream"
}
}
}
}
示例11: Compress
/// <summary>
/// Compresses an array of bytes.
/// </summary>
/// <param name="bytes">The input bytes.</param>
/// <returns>Returns the compressed bytes.</returns>
/// <exception cref="System.ArgumentNullException">bytes</exception>
/// <exception cref="ArgumentNullException">The <paramref name="bytes" /> parameter is null.</exception>
public static byte[] Compress(byte[] bytes)
{
if (bytes == null)
throw new ArgumentNullException("bytes");
using (var inputStream = new MemoryStream(bytes))
using (var outputStream = new MemoryStream())
{
using (var archive = new ZipArchive(outputStream, ZipArchiveMode.Create, false, null))
using (ZipArchiveEntry entry = archive.CreateEntry(DataEntryName))
using (Stream entryStream = entry.Open())
{
inputStream.CopyTo(entryStream);
}
return outputStream.ToArray();
}
}
示例12: UpdateReadTwice
public static async Task UpdateReadTwice()
{
using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(zfile("small.zip")), ZipArchiveMode.Update))
{
ZipArchiveEntry entry = archive.Entries[0];
string contents1, contents2;
using (StreamReader s = new StreamReader(entry.Open()))
{
contents1 = s.ReadToEnd();
}
using (StreamReader s = new StreamReader(entry.Open()))
{
contents2 = s.ReadToEnd();
}
Assert.Equal(contents1, contents2);
}
}
示例13: WriteFilesRecursiveToZip
private static void WriteFilesRecursiveToZip(ZipArchive archive, IStorage storage, string basePath)
{
string searchPattern = basePath;
string[] fileNames = storage.GetFileNames(searchPattern);
foreach (string fileName in fileNames)
{
Stream fileStream = storage.OpenFile(basePath + "/" + fileName, StorageFileMode.Open,
StorageFileAccess.Read);
archive.AddEntry(fileName, fileStream);
}
string[] directrryNames = storage.GetDirectoryNames(searchPattern);
foreach (string directoryName in directrryNames)
{
WriteFilesRecursiveToZip(archive, storage, basePath + "/" + directoryName);
}
}
示例14: ButtonOpenClick
private void ButtonOpenClick(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Zip File | *.zip";
bool? dialogResult = dialog.ShowDialog();
if (dialogResult == true)
{
#if WPF
Stream stream = dialog.OpenFile();
#else
Stream stream = dialog.File.OpenRead();
#endif
using (ZipArchive zipArchive = new ZipArchive(stream))
{
OpenedFileList.ItemsSource = zipArchive.Entries;
}
}
}
示例15: CreateZip
void CreateZip()
{
Console.WriteLine("creating zip file");
// Needed contents at ZipPath:
// Files\Application\ --> All contents requied for Programm Files (x86)\Sharpkit\
// Files\NET\ --> the content of FrameworkDir\SharpKit
// Files\NET_Unix\ --> only the modified files for unix. At this time, it will contain only the file SharpKit.Build.targets.
// Files\Templates\ --> contains 'SharpKit Web Application.zip' and 'SharpKit 5 Web Application.zip'
// Just copy the needed above files to @SourceFilesDir() and run this code.
// @SourceFilesDir must be the parent directory of Files\
using (var zip = new ZipArchive(ZipPath) { AddFileCallback = t => Console.WriteLine(t) })
{
zip.BeginUpdate();
zip.AddDirectory(SourceFilesDir);
zip.EndUpdate();
}
}