C# 将zip文件的内容作为StorageFile对象读取而不提取?

C# 将zip文件的内容作为StorageFile对象读取而不提取?,c#,uwp,zip,storagefile,C#,Uwp,Zip,Storagefile,还在做我的幻灯片项目!不太破旧,它现在可以递归读取目录,按顺序或随机顺序显示文件,放大和缩小,响应键盘和鼠标输入 我现在尝试让它读取zip文件的内容,而不解压缩它。当我递归读取用户作为输入给定的根目录时,它将遇到的所有文件存储为StorageFile对象列表。我希望能够读取zip文件的内容,并将其文件(即图像)添加到StorageFile列表中,然后最终打开并提取一个给定的文件(如果它是顺序或随机顺序中的下一个) 到目前为止,我发现可以提取磁盘上的文件,或者将zip文件的内容作为字符串列表读取

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

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

到目前为止,我发现可以提取磁盘上的文件,或者将zip文件的内容作为字符串列表读取

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


谢谢!:)

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

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... 

        }

    }
公共类ZipItem
{
公共存储文件主文件{get;set;}//源文件
源文件中的public ZipArchiveEntry motherentry{get;set;}/压缩文件
公共字符串名称{get;set;}
公共字符串实际字节{get;set;}
公共字符串压缩字节{get;set;}
}
公开名单结果//所有ZipItems的列表(Zipfile的所有元素,用于操作)
public int numfiles=0;//文件总数
公共整数i=0//指向列表中当前元素的指针(用于幻灯片放映)
私有异步无效下一次单击(对象发送方,路由目标)
{
Stream Stream=wait results[i].motherfile.OpenStreamForReadAsync();//创建一个流……我知道这是错误的类型(请参阅下面的错误)
ZipArchive archive=new ZipArchive(stream,ZipArchiveMode.Update);//打开ZipArchive。这会给我一个错误,告诉我“更新模式需要具有读、写和查找权限的流。
ZipArchiveEntry=results[i].motherentry;//这就是我被卡住的地方……我如何告诉它打开zip文件中的特定条目?
使用(var fileStream=entry.Open())//如何将其作为irandomaccesstream打开?
//使用(irandomaccesstream fileStream=results[i].motherentry.Open());
{
BitmapImage BitmapImage=新的BitmapImage();
bitmapImage.DecodePixelHeight=(int)ctlImage.Height;
bitmapImage.DecodePixelWidth=(int)ctlImage.Width;
bitmapImage.SetSource(filestream);//我知道我需要一个irandomaccesstream
ctlImage.Source=bitmapImage;//希望显示图像。。。
}
}
好的……明白了

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

其次,无法将ZipArchiveEntry读入iRandomaccesstream,我不知道为什么…在决定先在内存中读取它之前,先将它摆弄一段时间。因为我所做的是读取要显示在屏幕上的图像,所以它对内存管理的影响有限。但是,如果您正在读取较大的内容,请注意大小。我会检查它但是,为了简单起见,下面是将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;
        }
    }
}
公共类ZipItem
{
公共存储文件主文件{get;set;}
公共ZipArchiveEntry motherentry{get;set;}
公共字符串名称{get;set;}
公共字符串实际字节{get;set;}
公共字符串压缩字节{get;set;}
}
公开名单结果;
公共整数i=0;
私有异步无效下一次单击(对象发送方,路由目标)
{
//打开zip文件。我以前存储了所有zip文件,并将它们存储在“结果”列表中,该列表是用另一种方法填充的zip文件
Stream=等待结果[i].motherfile.OpenStreamForReadAsync();
使用(ZipArchive archive=new ZipArchive(stream,ZipArchiveMode.Read))
{
//查找条目“i”,这是我当前的幻灯片放映位置
ZipArchiveEntry entry=archive.GetEntry(结果[i].motherentry.Name);
//将条目作为流打开
Stream fileStream=entry.Open();
//在我在内存中阅读之前,我会检查条目的长度,以确保它不会太大。
//为了简单起见,这里我将显示“在内存中读取”部分
var memStream=new MemoryStream();
等待fileStream.CopyToAsync(memStream);
//将流位置重置为“开始”
memStream.Position=0;
//从memStream创建BitmapImage;
BitmapImage BitmapImage=新的BitmapImage();
bitmapImage.DecodePixelHeight=(int)ctlImage.Height;
bitmapImage.DecodePixelWidth=(int)ctlImage.Width;
//将位图图像的源设置为内存流
bitmapImage.SetSource(memStream.AsRandomAccessStream());
//将Image.Source设置为BitmapImage
ctlImage.Source=bitmapImage;
}
}
}

您遇到了什么问题?到目前为止,您尝试了什么?我正在寻找一个类,它可以为我提供从zip文件中读取而不提取文件的功能,并可以将zip内容的每个元素作为存储文件来读取。不知怎的,我能找到的所有东西,包括ZipArchive类,都可以读取fi列表从档案中提取其中一个。用于空间和性能