Ejemplo n.º 1
0
        public ZipReturn ZipFileCreate(string newFilename)
        {
            if (ZipOpen != ZipOpenType.Closed)
            {
                return(ZipReturn.ZipFileAlreadyOpen);
            }

            CompressUtils.CreateDirForFile(newFilename);
            _fileInfo = new FileInfo(newFilename);

            int errorCode = FileStream.OpenFileWrite(newFilename, out _inStream);

            if (errorCode != 0)
            {
                ZipFileClose();
                return(ZipReturn.ZipErrorOpeningFile);
            }
            ZipOpen = ZipOpenType.OpenWrite;
            return(ZipReturn.ZipGood);
        }
Ejemplo n.º 2
0
        public ZipReturn ZipFileCreate(string newFilename, SevenZipCompressType compressOutput, int dictionarySize = 1 << 24, int numFastBytes = 64)
        {
            if (ZipOpen != ZipOpenType.Closed)
            {
                return(ZipReturn.ZipFileAlreadyOpen);
            }

            CompressUtils.CreateDirForFile(newFilename);
            _zipFileInfo = new FileInfo(newFilename);

            int errorCode = FileStream.OpenFileWrite(newFilename, out _zipFs);

            if (errorCode != 0)
            {
                ZipFileClose();
                return(ZipReturn.ZipErrorOpeningFile);
            }
            ZipOpen = ZipOpenType.OpenWrite;

            _signatureHeader = new SignatureHeader();
            _header          = new Header();

            using (BinaryWriter bw = new(_zipFs, Encoding.UTF8, true))
            {
                _signatureHeader.Write(bw);
            }
            _baseOffset = _zipFs.Position;


            _packedOutStreams = new List <outStreams>();

            _compType = compressOutput;

#if solid
            outStreams newStream = new()
            {
                packedStart     = (ulong)_zipFs.Position,
                compType        = compressOutput,
                packedSize      = 0,
                unpackedStreams = new List <UnpackedStreamInfo>()
            };
            switch (compressOutput)
            {
            case SevenZipCompressType.lzma:
                LzmaEncoderProperties ep  = new(true, dictionarySize, numFastBytes);
                LzmaStream            lzs = new(ep, false, _zipFs);

                newStream.Method     = new byte[] { 3, 1, 1 };
                newStream.Properties = lzs.Properties;
                _compressStream      = lzs;
                break;

            case SevenZipCompressType.zstd:
                ZstdSharp.CompressionStream zss = new(_zipFs, 19);
                newStream.Method     = new byte[] { 4, 247, 17, 1 };
                newStream.Properties = new byte[] { 1, 5, 19, 0, 0 };
                _compressStream      = zss;
                break;

            case SevenZipCompressType.uncompressed:
                newStream.Method     = new byte[] { 0 };
                newStream.Properties = null;
                _compressStream      = _zipFs;
                break;
            }

            _packedOutStreams.Add(newStream);
#endif
            return(ZipReturn.ZipGood);
        }
Ejemplo n.º 3
0
        public static void extract(string dirName, string outPath)
        {
            if (CommandFindRomsInGame == null)
            {
                CommandFindRomsInGame = new SQLiteCommand(
                    @"SELECT
                    ROM.RomId, ROM.name, FILES.size, FILES.compressedsize, FILES.crc, FILES.sha1
                 FROM ROM,FILES WHERE ROM.FileId=FILES.FileId AND ROM.GameId=@GameId AND ROM.PutInZip ORDER BY ROM.RomId", DBSqlite.db.Connection);
            }
            CommandFindRomsInGame.Parameters.Add(new SQLiteParameter("GameId"));

            Debug.WriteLine(dirName);

            SQLiteCommand getfiles = new SQLiteCommand(@"SELECT dir.FullName,GameId,game.Name FROM dir,dat,game where dat.dirid=dir.dirid and game.datid=dat.datid and dir.fullname like '" + dirName + "%'", DBSqlite.db.Connection);

            DbDataReader reader = getfiles.ExecuteReader();

            while (reader.Read())
            {
                string outputFile = reader["fullname"].ToString() + reader["Name"].ToString() + ".zip";
                outputFile = outputFile.Substring(dirName.Length);

                outputFile = Path.Combine(outPath, outputFile).Replace(@"/", @"\");

                Debug.WriteLine(outputFile);

                int    GameId   = Convert.ToInt32(reader["GameId"]);
                string GameName = reader["name"].ToString();
                Debug.WriteLine("Game " + GameId + " Name: " + GameName);

                Zip memZip = new Zip();
                memZip.ZipCreateFake();

                ulong fileOffset = 0;

                Stream zipFs = null;

                int romCount = 0;
                using (DbDataReader drRom = ZipSetGetRomsInGame(GameId))
                {
                    while (drRom.Read())
                    {
                        int    RomId          = Convert.ToInt32(drRom["RomId"]);
                        string RomName        = drRom["name"].ToString();
                        ulong  size           = Convert.ToUInt64(drRom["size"]);
                        ulong  compressedSize = Convert.ToUInt64(drRom["compressedsize"]);
                        byte[] CRC            = VarFix.CleanMD5SHA1(drRom["crc"].ToString(), 8);
                        byte[] sha1           = VarFix.CleanMD5SHA1(drRom["sha1"].ToString(), 32);

                        Debug.WriteLine("    Rom " + RomId + " Name: " + RomName + "  Size: " + size + "  Compressed: " + compressedSize + "  CRC: " + VarFix.ToString(CRC));

                        byte[] localHeader;
                        memZip.ZipFileAddFake(RomName, fileOffset, size, compressedSize, CRC, out localHeader);

                        //ZipSetLocalFileHeader(RomId, localHeader, fileOffset);
                        if (romCount == 0)
                        {
                            CompressUtils.CreateDirForFile(outputFile);
                            int errorCode = FileStream.OpenFileWrite(outputFile, out zipFs);
                        }
                        zipFs.Write(localHeader, 0, localHeader.Length);

                        gZip   GZip        = new gZip();
                        string strFilename = RomRootDir.Getfilename(sha1);
                        GZip.ZipFileOpen(strFilename, -1, true);
                        GZip.ZipFileOpenReadStream(0, true, out Stream oStr, out ulong _);

                        StreamCopier.StreamCopy(oStr, zipFs, compressedSize);

                        GZip.ZipFileCloseReadStream();
                        GZip.ZipFileClose();

                        fileOffset    += (ulong)localHeader.Length + compressedSize;
                        zipFs.Position = (long)fileOffset;

                        romCount += 1;
                    }
                }

                memZip.ZipFileCloseFake(fileOffset, out byte[] centeralDir);

                if (romCount > 0)
                {
                    zipFs.Write(centeralDir, 0, centeralDir.Length);
                    zipFs.Flush();
                    zipFs.Close();
                    zipFs.Dispose();
                }
            }
        }