c#实现metro文件压缩解压示例

时间:2022-07-07 22:36:33 

在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();
                                    }
                                }
                            }
}

标签:metro,压缩,解压
0
投稿

猜你喜欢

  • Android Drawable必备知识小结

    2021-09-09 20:56:16
  • Java 高并发十: JDK8对并发的新支持详解

    2022-12-02 02:43:09
  • 使用Postman传递arraylist数据给springboot方式

    2022-08-27 01:13:01
  • IDEA自定义pom依赖的步骤详解

    2021-11-06 04:14:32
  • 浅谈BeanPostProcessor加载次序及其对Bean造成的影响分析

    2022-05-02 19:52:29
  • C#中Winform窗体Form的关闭按钮变灰色的方法

    2023-06-06 09:11:34
  • C#中的枚举类型(Enum)介绍

    2023-06-23 18:20:06
  • JavaMailSender实现邮箱验证功能

    2022-05-11 12:32:59
  • Android Webview滑进出屏幕闪烁的解决方法

    2021-11-30 11:57:33
  • 处理java异步事件的阻塞和非阻塞方法分析

    2023-04-16 05:06:01
  • SpringBoot中使用Filter和Interceptor的示例代码

    2022-06-28 17:20:04
  • 使用SpringBoot 工厂模式自动注入到Map

    2021-12-22 10:02:42
  • Android10.0实现本地音乐播放(附源码下载)

    2022-12-05 20:29:40
  • 在Android中如何使用DataBinding详解(Kotlin)

    2022-12-17 15:09:51
  • C#使用读写锁解决多线程并发问题

    2022-02-28 20:36:09
  • Android实现可复用的筛选页面

    2023-02-23 09:41:27
  • C#实现将记事本中的代码编译成可执行文件的方法

    2022-03-05 02:17:53
  • redisson分布式限流RRateLimiter源码解析

    2021-05-29 13:10:15
  • Springboot中加入druid连接池

    2023-08-08 05:47:22
  • Spring自定义注解配置简单日志示例

    2023-01-25 09:31:08
  • asp之家 软件编程 m.aspxhome.com