Unzipping 7z in memory #352

Closed
opened 2026-01-29 22:10:33 +00:00 by claunia · 2 comments
Owner

Originally created by @beho1986 on GitHub (Apr 22, 2019).

I have a 7z that I have embedded in my resources and trying to unzip and read in memory. I used ionic previously with a zip file not a 7z. Here is my code and it's not working, as I am having an issue with opening the zipped file:

Using zipFileStream As MemoryStream = New MemoryStream(My.Resources.Scores)
   Using zip1 As ZipArchive = ZipArchive.Open(zipFileStream)
       Dim zipped_entry As ZipArchive
       Dim filename_holder As String = ""
       For Each zipped_entry In zip1
           filename_holder = zipped_entry.FileName.Replace("-", "_")
           If filename_holder.Contains(RTFlinky_AR) Then
               Dim msss As New MemoryStream
               zipped_entry.ExtractWithPassword(msss,"qqq123")
               Dim buffer As Byte() = Encoding.UTF8.GetBytes(msss.ReadByte)
               msss.Write(buffer, 0, buffer.Length)
               msss.Seek(0, SeekOrigin.Begin)
               Score_Richtext.LoadFile(msss, RichTextBoxStreamType.RichText)
               Exit For
           End If
       Next
   End Using
   End Using
Originally created by @beho1986 on GitHub (Apr 22, 2019). I have a 7z that I have embedded in my resources and trying to unzip and read in memory. I used ionic previously with a zip file not a 7z. Here is my code and it's not working, as I am having an issue with opening the zipped file: ``` Using zipFileStream As MemoryStream = New MemoryStream(My.Resources.Scores) Using zip1 As ZipArchive = ZipArchive.Open(zipFileStream) Dim zipped_entry As ZipArchive Dim filename_holder As String = "" For Each zipped_entry In zip1 filename_holder = zipped_entry.FileName.Replace("-", "_") If filename_holder.Contains(RTFlinky_AR) Then Dim msss As New MemoryStream zipped_entry.ExtractWithPassword(msss,"qqq123") Dim buffer As Byte() = Encoding.UTF8.GetBytes(msss.ReadByte) msss.Write(buffer, 0, buffer.Length) msss.Seek(0, SeekOrigin.Begin) Score_Richtext.LoadFile(msss, RichTextBoxStreamType.RichText) Exit For End If Next End Using End Using
Author
Owner

@Kim-SSi commented on GitHub (May 25, 2019):

This is written in C# based on your above code. Please convert it to VB.
Please see the wiki for basic operations.
https://github.com/adamhathcock/sharpcompress/wiki/API-Examples

using (var archiveStream = new MemoryStream(My.Resources.Scores))
{
    //Options for opening the archive
    var options = new ReaderOptions();
    options.Password = "qqq123";
    //ArchiveFactory opens any supported archive for random access using options
    var archive = ArchiveFactory.Open(archiveStream, options);
    foreach (var entry in archive.Entries)
    {
        if (entry.IsDirectory)
        {
            //Skip if it is a Directory
            continue;
        }
        var filenameHolder = entry.Key.Replace("-", "_");
        if (filenameHolder.Contains(RTFlinky_AR))
        {
            //The desired file has been located, extract it
            using (var msss = new MemoryStream())
            {
                entry.WriteTo(msss);
                msss.Position = 0;
                Score_Richtext.LoadFile(msss, RichTextBoxStreamType.RichText);
                return;
            }
        }
    }
}
@Kim-SSi commented on GitHub (May 25, 2019): This is written in C# based on your above code. Please convert it to VB. Please see the wiki for basic operations. [https://github.com/adamhathcock/sharpcompress/wiki/API-Examples](https://github.com/adamhathcock/sharpcompress/wiki/API-Examples) ``` using (var archiveStream = new MemoryStream(My.Resources.Scores)) { //Options for opening the archive var options = new ReaderOptions(); options.Password = "qqq123"; //ArchiveFactory opens any supported archive for random access using options var archive = ArchiveFactory.Open(archiveStream, options); foreach (var entry in archive.Entries) { if (entry.IsDirectory) { //Skip if it is a Directory continue; } var filenameHolder = entry.Key.Replace("-", "_"); if (filenameHolder.Contains(RTFlinky_AR)) { //The desired file has been located, extract it using (var msss = new MemoryStream()) { entry.WriteTo(msss); msss.Position = 0; Score_Richtext.LoadFile(msss, RichTextBoxStreamType.RichText); return; } } } } ```
Author
Owner

@beho1986 commented on GitHub (May 25, 2019):

Thanks a lot Kim, it worked flawlessly, I will continue readying the examples.

@beho1986 commented on GitHub (May 25, 2019): Thanks a lot Kim, it worked flawlessly, I will continue readying the examples.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#352