Solved

Trying to extract a file from a zip file (SharpZLib) using a Memory Stream, and then save the entry as a byte array

Posted on 2007-11-19
5
3,742 Views
Last Modified: 2013-12-17
Hello please could someone help me try and make sense of SharpZLib. What I am trying to do is extract a file from a list of files, and then saving this entry as a byte[]. The problem is I am having problems actually working out how to do this using the component. Most examples I have looked at relate to saving the entry to disk, and this is not possible as I am using ASP 2.0.

This is my code as it stands:

zipFile = new ICSharpCode.SharpZipLib.Zip.ZipFile( this.edtFilename.PostedFile.InputStream );
foreach ( ZipEntry entry in zipFile )
{
    Stream stream = new MemoryStream();
    stream = zipFile.GetInputStream(entry);
    Byte[] bytes;
    bytes = ReadFully(stream);
}

However when I use this example code, the GetInputStream stream length is 0. The output stream has to be stored in memory as we write the zip entry file as byte[] to the database.

Any help is appreciated, thanks.
0
Comment
Question by:Link-HRSystems
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:gocemi
ID: 20312065
Hi Extracting only one file from the zip archive,
If you want to extract all it is very similar:
Code:


 ICSharpCode.SharpZipLib.Zip.ZipInputStream zis = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(new System.IO.FileStream("PATH TO ZIP ARCHIVE", System.IO.FileMode.Open));

            ICSharpCode.SharpZipLib.Zip.ZipEntry entry = null;

            while (  (entry = zis.GetNextEntry()) != null)

            {

                if (entry.Name.ToLower() == "FILENAME THAT YOU LIKE TO EXTRACT")

                {

                    //lets extract

                    System.IO.FileStream fs = new System.IO.FileStream("PATH WHERE TO EXTRACT" + entry.Name, System.IO.FileMode.Append);

                    int size = 2048;

                    byte[] data = new byte[2048];

                    while (true)

                    {

                        size = zis.Read(data, 0, data.Length);

                        if (size > 0)

                        {

                            fs.Write(data, 0, size);

                        }

                        else

                        {

                            break;

                        }

                    }
 

                    fs.Close();
 

                    fs.Dispose();

                }

            }

Open in new window

0
 

Author Comment

by:Link-HRSystems
ID: 20312391
Hi, the example works for a FileStream though. I am unable to use a file stream due to the application being ASP.net based. The problem I am having is trying to get the output stream from an extracted file and all examples I have looked at, seem to rely on the output being a file stream. Is it possible you could convert the above method to work with MemoryStreams only? Thanks.
0
 
LVL 5

Accepted Solution

by:
gocemi earned 500 total points
ID: 20312495
Hope this will hlp

 System.IO.MemoryStream ms = new System.IO.MemoryStream();

            System.Text.StringBuilder sb = new StringBuilder();

            

            ICSharpCode.SharpZipLib.Zip.ZipInputStream zis = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(new System.IO.FileStream("PATH TO ZIP ARCHIVE", System.IO.FileMode.Open));

            ICSharpCode.SharpZipLib.Zip.ZipEntry entry = null;

            while (  (entry = zis.GetNextEntry()) != null)

            {

                if (entry.Name.ToLower() == "FILE YOU ARE SEARCHING FOR EXTRACT")

                {

                    //lets extract

                    //System.IO.FileStream fs = new System.IO.FileStream("PATH WHERE TO EXTRACT" + entry.Name, System.IO.FileMode.Append);

                   

                    int size = 2048;

                    byte[] data = new byte[2048];

                    while (true)

                    {

                        size = zis.Read(data, 0, data.Length);

                        if (size > 0)

                        {

                            ms.Write(data, 0, size);

                        }

                        else

                        {

                            break;

                        }

                    }
 

                    //fs.Close();
 

                    //fs.Dispose();

                }

            }
 

            byte[] bb = ms.ToArray();

            string content = System.Text.Encoding.[Encoding USED IN FILE].GetString(bb);

Open in new window

0
 

Author Closing Comment

by:Link-HRSystems
ID: 31409918
That's just what I needed, thank you. Streams totally baffle me sometimes :)
0
 

Author Comment

by:Link-HRSystems
ID: 20312840
That's just what I needed, thank you. Streams totally baffle me sometimes :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WKHTMLTOPDF - --disable-smart-shrinking not working 10 37
Get id from json Data with NewtonSoft 3 34
SSL https .net web site force redirect 3 37
Error in script 11 47
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now