c#實現metro文件壓縮解壓示例
在1.zip中增加一張新圖片
StorageFile jpg = await KnownFolders.PicturesLibrary.GetFileAsync("1.jpg");
StorageFile zip = await KnownFolders.PicturesLibrary.GetFileAsync("1.zip");
//把上面這句改成如下就成了壓縮文件
//StorageFile zip = await KnownFolders.PicturesLibrary.CreateFileAsync(jpg.DisplayName+".zip",CreationCollisionOption.ReplaceExisting);
using (ZipArchive archive = new ZipArchive((await zip.OpenAsync(FileAccessMode.ReadWrite)).AsStream(), ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry = archive.CreateEntry(jpg.Name);
byte[] buffer = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(jpg));
using (var writer = readmeEntry.Open())
{
await writer.WriteAsync(buffer, 0, buffer.Length);
}
}
把1.jpg從1.zip中刪除
StorageFile zip = await KnownFolders.PicturesLibrary.GetFileAsync("1.zip");
using (ZipArchive archive = new ZipArchive((await zip.OpenAsync(FileAccessMode.ReadWrite)).AsStream(), ZipArchiveMode.Update))
{
//刪除文件
archive.GetEntry("1.jpg").Delete();
}
導出1.jpg,newFile為要到出的文件
StorageFile zip = await KnownFolders.PicturesLibrary.GetFileAsync("1.zip");
using (ZipArchive archive = new ZipArchive((await zip.OpenAsync(FileAccessMode.ReadWrite)).AsStream(), ZipArchiveMode.Update))
{
ZipArchiveEntry zipArchiveEntry = archive.GetEntry("1.jpg").
using (Stream fileData = zipArchiveEntry.Open())
{
StorageFile newFile = await KnownFolders.PicturesLibrary.CreateFileAsync(zipArchiveEntry.FullName, CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream newFileStream = await newFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (Stream s = newFileStream.AsStreamForWrite())
{
await fileData.CopyToAsync(s);
await s.FlushAsync();
}
}
}
}
相關文章
Winform學生信息管理系統(tǒng)各子窗體剖析(3)
這篇文章主要針對Winform學生信息管理系統(tǒng)各子窗體進行剖析,感興趣的小伙伴們可以參考一下2016-05-05C#實現winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法
這篇文章主要介紹了C#實現winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法,涉及C#窗體交互的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07