欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 08:11:04
在1.zip中增加一张新图片复制代码 代码如下:storagefile jpg = await knownfolders.pictureslibrary.getfileas...

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

上一篇:

下一篇: