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

Esta ·
更新时间:2024-11-15
· 838 次阅读

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

您可能感兴趣的文章:C#使用GZipStream解压缩数据文件的方法c# 文件压缩zip或将zip文件解压的方法C#实现文件压缩与解压的方法示例【ZIP格式】ASP.NET 文件压缩解压类(C#)C#实现的文件压缩和解压缩类C#使用iCSharpcode进行文件压缩实现方法C#文件流进行压缩和解压缩的方法C#实现rar压缩与解压缩文件的方法c#解压文件的实例方法C#使用GZipStream实现文件的压缩与解压



压缩 C# 示例 metro 解压

需要 登录 后方可回复, 如果你还没有账号请 注册新账号