在UWP中从归档中提取特定文件

问题描述 投票:0回答:2

为了节省空间,我在我的UWP项目中压缩了我的书(xml格式)。我想根据文件的名称将文件解压缩到我的本地文件夹。

直到现在我做了什么(这提取所有文件):

ZipFile.ExtractToDirectory(sourceCompressedFile.Path, destinationFolder.Path);

但是,这会将我的存档中的所有文件提取到目标文件夹。我知道这可能是一个使用SharpZipLib的简单任务,但这是一个内置的方法,可以帮助我减少我的应用程序大小。我只想提取一个名称与我提供的名称相匹配的文件。除此之外还有三种方法,但我迷失了方法。

这可以使用DotnetZip轻松完成,如here所见,但我不想使用任何第三方库

c# zip win-universal-app
2个回答
0
投票

我认为你在一个zip存档中压缩了几个文件,因此ZipFile.ExtractToDirectory Method (String, String)会将指定zip存档中的所有文件解压缩到一个目录中。

如果您只想访问此压缩存档中的一个特殊文件,可以使用ZipArchiveEntry Class来完成此工作,例如:

StorageFolder _folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
/*fails here FileNotFound*/
StorageFile sourceCompressedFile = await _folder.GetFileAsync("archived.zip");

StorageFolder folder = ApplicationData.Current.LocalFolder;

// ZipFile.ExtractToDirectory(file.Path, folder.Path);

using (ZipArchive archive = ZipFile.OpenRead(sourceCompressedFile.Path))
{
    foreach (ZipArchiveEntry entry in archive.Entries)
    {
        if (entry.FullName.ToString() == "miao2.jpg")
        {
            entry.ExtractToFile(Path.Combine(folder.Path, entry.FullName));
        }
    }
}

我将几张图片压缩为“archived.zip”文件进行测试,在此示例中,它只会提取名为“miao2.jpg”的图像文件。


1
投票

好的,我知道了!

首先,要读取特定条目,请使用ZipArchiveEntry.GetEntry(entrytosearch);

第二,无法将ZipArchiveEntry读入IRandomAccessStream,我不知道为什么......在决定先在内存中读取它之前,已经摆弄了一段时间。由于我正在做的是读取要在屏幕上显示的图像,因此它对内存管理的影响有限。但是,如果你正在阅读一些大的东西,请注意它的大小。在阅读之前,我会检查条目的.Length。但是,出于简单的目的,这里是更新的代码,用于将zip文件的特定“.jpg”条目读入Image.Source。

不优雅或复杂,但我希望它能节省一些时间,我花时间摆弄这个!

    public class ZipItem
    {
        public StorageFile motherfile { get; set; }
        public ZipArchiveEntry motherentry { get; set; }
        public string Name { get; set; }
        public string ActualBytes { get; set; }
        public string CompressedBytes { get; set; }
    }

    public List<ZipItem> results;
    public int i = 0;

    private async void  nextimg_Click(object sender, RoutedEventArgs e)
    {
        //Open the zip file. At that point in the program, I previously read
        //all zip files in a hierarchy and stored them in the "results" a 
        //list of ZipItems populated in another method. i points to the
        //image I wish to display in the list. 

        Stream stream = await results[i].motherfile.OpenStreamForReadAsync();

        using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read))
        {
            //look for the entry "i", which is my current slideshow position
            ZipArchiveEntry entry = archive.GetEntry(results[i].motherentry.Name);

            //Open the entry as a stream
            Stream fileStream = entry.Open();

            //Before I read this in memory, I do check for entry.Length to make sure it's not too big. 
            //For simplicity purposes here, I jsut show the "Read it in memory" portion
            var memStream = new MemoryStream();
            await fileStream.CopyToAsync(memStream);
            //Reset the stream position to start
            memStream.Position = 0;

            //Create a BitmapImage from the memStream;
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.DecodePixelHeight = (int)ctlImage.Height;
            bitmapImage.DecodePixelWidth = (int)ctlImage.Width;

            //Set the source of the BitmapImage to the memory stream
            bitmapImage.SetSource(memStream.AsRandomAccessStream());

            //Set the Image.Source to the BitmapImage
            ctlImage.Source = bitmapImage;
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.