Exemple #1
0
        public void Extract(
            FileStream inStream,
            ArchiveDatabaseEx db,
            uint fileIndex,
            out uint blockIndex,
            out byte[] outBuffer,
            out ulong outBufferSize,
            ref ulong offset,
            ref ulong outSizeProcessed)
        {
            //Console.WriteLine("Extract >>>");
            uint folderIndex = db.FileIndexToFolderIndexMap[fileIndex];
            offset = 0;
            outSizeProcessed = 0;
            outBuffer = null;
            if (folderIndex == UInt32.MaxValue)
            {
                //Console.WriteLine("folderindex is UInt32.MaxValue");
                blockIndex = folderIndex;
                outBuffer = null;
                outBufferSize = 0;
                return;
            }

            Folder folder = db.Database.Folders[folderIndex];
            ulong unPackSize = folder.GetUnPackSize();
            //#ifndef _LZMA_IN_CB
            ulong packSize = db.GetFolderFullPackSize(folderIndex);
            //Console.WriteLine("packsize: " + packSize + " unpacksize: " + unPackSize );
            //Byte *inBuffer = 0;
            //size_t processedSize;
            //#endif
            blockIndex = folderIndex;
            outBuffer = null;

            //Console.WriteLine("folderstreampos: " + db.GetFolderStreamPos(folderIndex, 0));
            inStream.Seek((long)db.GetFolderStreamPos(folderIndex, 0), SeekOrigin.Begin);
            //RINOK(inStream->Seek(inStream, SzArDbGetFolderStreamPos(db, folderIndex, 0)));

            //#ifndef _LZMA_IN_CB
            //if (packSize != 0)
            //{
            // inBuffer = (Byte *)allocTemp->Alloc((size_t)packSize);
            // if (inBuffer == 0)
            //   return SZE_OUTOFMEMORY;
            // }
            // res = inStream->Read(inStream, inBuffer, (size_t)packSize, &processedSize);
            // if (res == SZ_OK && processedSize != (size_t)packSize)
            //  res = SZE_FAIL;
            //#endif
            outBufferSize = unPackSize;
            if (unPackSize != 0)
            {
                outBuffer = new byte[unPackSize];
                //*outBuffer = (Byte *)allocMain->Alloc((size_t)unPackSize);
                //if (*outBuffer == 0)
                //res = SZE_OUTOFMEMORY;
            }
            ulong outRealSize;
            Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(folder.Coders[0].Properties);
            Stream outStream = new MemoryStream(outBuffer);
            decoder.Code(inStream, outStream, (long)packSize, (long)unPackSize, null);
            //SzDecode(db.Database.PackSizes +
            // db.FolderStartPackStreamIndex[folderIndex], folder,
            //  //#ifdef _LZMA_IN_CB
            // inStream,
            // //#else
            // //inBuffer,
            //   //#endif
            //outBuffer, unPackSize, out outRealSize);
            outStream.Close();
            //Console.WriteLine( Encoding.UTF8.GetString( outBuffer, 0, (int)unPackSize ) );
            outRealSize = unPackSize;
            if (outRealSize == unPackSize)
            {
                //if (folde.UnPackCRCDefined)
                //{
                // if (!CrcVerifyDigest(folder->UnPackCRC, *outBuffer, (size_t)unPackSize))
                //  res = SZE_FAIL;
                //}
            }
            else
            {
                throw new Exception("Unpack size was different from packsize: " + unPackSize + " vs " + outRealSize);
                //res = SZE_FAIL;
            }
            UInt32 i;
            FileItem fileItem = db.Database.Files[fileIndex];
            offset = 0;
            for (i = db.FolderStartFileIndex[folderIndex]; i < fileIndex; i++)
            {
                offset += (UInt32)db.Database.Files[i].Size;
            }
            outSizeProcessed = fileItem.Size;
            if (offset + outSizeProcessed > outBufferSize)
            {
                throw new Exception("offset + outsizeprocessed > outbuffersize " + offset + " + " + outSizeProcessed + " > " + outBufferSize);
                // return SZE_FAIL;
            }
            //if (fileItem->IsFileCRCDefined)
            //{
            //   if (!CrcVerifyDigest(fileItem->FileCRC, *outBuffer + *offset, *outSizeProcessed))
            //      res = SZE_FAIL;
            //}
        }
Exemple #2
0
        public static Dictionary<string, byte[]> ExtractFolder(
            FileStream inStream,
            ArchiveDatabaseEx db,
            uint folderIndex)
        {
            Dictionary<string, byte[]> fileDict = new Dictionary<string, byte[]>();

            ulong packSize = db.GetFolderFullPackSize(folderIndex);
            Folder folder = db.Database.Folders[folderIndex];
            ulong unPackSize = folder.GetUnPackSize();
            //byte[] outBuffer; = new byte[unPackSize];

            inStream.Seek((long)db.GetFolderStreamPos(folderIndex, 0), SeekOrigin.Begin);

            Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(folder.Coders[0].Properties);

            using (MemoryStream ms = new MemoryStream())
            {
                decoder.Code(inStream, ms, (long)packSize, (long)unPackSize, null);

                //Extract file
                FileItem[] fileItemList = db.Database.Files;
                long offset = 0;
                for (uint i = db.FolderStartFileIndex[folderIndex]; i < fileItemList.Length; i++)
                {
                    FileItem item = fileItemList[i];
                    if (item.IsDirectory)
                        continue;
                    byte[] tempBuf = new byte[item.Size];

                    if ((offset + (long)item.Size) > ms.Capacity)
                        throw new Exception("offset + filesize > length " + offset + " + " + item.Size + " > " + ms.Capacity);

                    ms.Seek(offset, SeekOrigin.Begin);
                    ms.Read(tempBuf, 0, tempBuf.Length);

                    string fileName = item.Name.Replace("\0", "");
                    fileDict.Add(fileName, tempBuf);

                    offset += (long)fileItemList[i].Size;
                }
            }

            return fileDict;
        }