c#實現(xiàn)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();
}
導(dǎo)出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();
}
}
}
}
相關(guān)文章
c#和javascript函數(shù)相互調(diào)用示例分享
在webBrowser使用過程中為了C#和JS通訊,webBrowser必須設(shè)置ObjectForScripting的屬性,它是一個object,這個object可以提供給webBrowser控件載入的網(wǎng)頁上的script訪問2014-01-01
winform壁紙工具為圖片添加當(dāng)前月的日歷信息
使用用winform做了一個設(shè)置壁紙小工具,為圖片添加當(dāng)月的日歷并設(shè)為壁紙,可以手動/定時設(shè)置壁紙,最主要的特點是在圖片上生成當(dāng)前月的日歷信息,感興趣的你可以參考下2013-03-03

