在不解压的情况下将 zip 文件的内容作为 StorageFile 对象读取?

Read the content of a zip file as a StorageFile object without extracting?

仍在处理我的幻灯片项目!还不错,它现在可以递归读取目录,按顺序或随机顺序显示文件,放大和缩小,响应键盘和鼠标输入。

我现在正在尝试让它在不解压缩的情况下读取 zip 文件的内容。当我递归读取用户作为输入给出的根目录时,它将遇到的所有文件存储为 StorageFile 对象列表。我希望能够读取 zip 文件的内容并将其文件(即图像)添加到 StorageFile 列表中,然后最终打开并提取单个给定文件(如果它是下一个顺序或随机顺序)。

到目前为止我所发现的允许提取磁盘上的文件或读取 zip 文件的内容作为字符串列表。

我清楚自己需要什么吗?知道怎么做吗?

谢谢! :)

好的,我已经打开了 zip 文件,阅读了它们的 ZipArchiveEntry 内容并在 ZipArchiveEntries 的内存中创建了一个列表。然而,我被困在两件事上:

1) 如何重新打开 ZipArchive 并访问列表中的特定 ZipArchiveEntry?

2) 如何将 .jpg 图像的 ZipArchiveEntry 读取到 BitmapImage 中以将其显示到 Image.Source 中?

这是一个代码示例,它无法正常工作!哈哈!

    public class ZipItem
    {
        public StorageFile motherfile { get; set; }  //Source File
        public ZipArchiveEntry motherentry { get; set; }  /Compressed file within the source file
        public string Name { get; set; }
        public string ActualBytes { get; set; }
        public string CompressedBytes { get; set; }
    }

    public List<ZipItem> results;      //List of all ZipItems (all elements of a Zipfile, for manipulation purposes)
    public int numfiles = 0;   // Total number of files 
    public int i = 0;   //Pointer to the current element in the list (for slideshow purposes)


    private async void  nextimg_Click(object sender, RoutedEventArgs e)
    {
        Stream stream = await results[i].motherfile.OpenStreamForReadAsync(); //Create a stream... I know this is the wrong type (see error below)

        ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Update);  //Open the ZipArchive. This gives me an error telling me that "Update mode requires a stream with Read, Write and Seek permission.

        ZipArchiveEntry entry = results[i].motherentry; //This is where I'm stuck.... how do I tell it to open a specific entry in the zip file?

        using (var fileStream = entry.Open()) //How do I open it as an IRandomAccessStream?
        //using (IRandomAccessStream fileStream = results[i].motherentry.Open());
        {

            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.DecodePixelHeight = (int)ctlImage.Height;
            bitmapImage.DecodePixelWidth = (int)ctlImage.Width;
            bitmapImage.SetSource(filestream); //I know I need a IRandomAccessStream
            ctlImage.Source = bitmapImage;   //Hopefully display the image... 

        }

    }

好的...知道了!

首先,要阅读特定条目,请使用 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. I previously stored all zip files and stored them in the "results" a list of ZipItems populated in another method
    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;
        }
    }
}