This project is read-only.

Extracting Files from a Stream to a Byte-Array

Sep 2, 2009 at 10:37 AM

Hi,

sorry my english is very bad, therefore i explain my problem very quickly.

in my function i get a byte-Array with one or more compressed Files. If i would like to extract this byte-array i use the following code

public Hashtable ExtractToFileList(byte[] Data)
        {
            #region local Variables
            Hashtable fileList = new Hashtable();
            MemoryStream memStream = new MemoryStream(Data);
            #endregion

            #region validate parameters
            if (Data == null || Data.Length == 0)
            {
                return null;
            }
            #endregion

            #region extract Zip-Archive
            SevenZipExtractor.SetLibraryPath(sevenZipPath);
            using (SevenZipExtractor extr = new SevenZipExtractor(memStream))
            {
                //MemoryStream ms = new MemoryStream();
                //extr.ExtractFile(0,ms);
                //ms.Position = 0;
                extr.ExtractFiles(new ExtractFileCallback(callBack));
                foreach (String archiveFileName in extr.ArchiveFileNames)
                {
                    fileList[archiveFileName] = ms.GetBuffer();
                }
            }
            return fileList;
            #endregion
        }
        private void callBack(SevenZip.ExtractFileCallbackArgs args)
        {
            //???????
        }

you can see, where my Problem is. What must i write into the callBack Function to get the decompressed Byte-Array. The ExtractToStream Property in the ExtractFileCallbackArgs is always null.

Have you got a example to work with the ExtractFileCallbackArgs?

Thanks for answer me.

Madis

Sep 2, 2009 at 11:19 AM

ok, i have got do another test with the ExtractFile Function.

            SevenZipExtractor.SetLibraryPath(sevenZipPath);
            using (SevenZipExtractor extr = new SevenZipExtractor(memStream))
            {
                MemoryStream ms = new MemoryStream();
                foreach (String file in extr.ArchiveFileNames)
                {
                    extr.ExtractFile(file, ms);
                    fileList[file] = ms.GetBuffer();
                }
            }
            return fileList;

after the extraction i have got 2 Files in my filelist. But i think there are still an exception, because the filesize of the first file are much bigger then the normal Size on the filesystem.

-        fileList    Count = 2    System.Collections.Hashtable
+        ["test2.docx"]    {byte[41121]}    
+        ["test.docx"]    {byte[10208]} 

On FileSystem the first document are 30.913 bytes and the second document is 10.208 bytes.

 

Sep 2, 2009 at 6:34 PM

41.121 = 30.913 + 10.208.

            SevenZipExtractor.SetLibraryPath(sevenZipPath);
            using (SevenZipExtractor extr = new SevenZipExtractor(memStream))
            {
                //This line is wrong
                //MemoryStream ms = new MemoryStream();
                foreach (String file in extr.ArchiveFileNames)
                {
                    //It must be here
                    MemoryStream ms = new MemoryStream();
                    extr.ExtractFile(file, ms);
                    fileList[file] = ms.GetBuffer();
                }
            }
            return fileList;
So you see, the second file (test2.docx in your case) is extracted to the same MemoryStream, so ms.GetBuffer() returns the wrong array.