コード例 #1
0
ファイル: VDrive.cs プロジェクト: vidarw/RVWorld
        private void copyStream(VFile.VZipFile source, byte[] destination, long sourceOffset, long destinationOffset, long sourceLength, long destinationLength)
        {
            // this is where to start reading in the source array
            long sourceStart;
            long destinationStart;

            if (sourceOffset < destinationOffset)
            {
                sourceStart      = destinationOffset - sourceOffset;
                destinationStart = 0;
                sourceLength    -= sourceStart;

                // check if source is all before destination
                if (sourceLength <= 0)
                {
                    return;
                }
            }
            else
            {
                sourceStart        = 0;
                destinationStart   = sourceOffset - destinationOffset;
                destinationLength -= destinationStart;

                // check if desination is all before source
                if (destinationLength <= 0)
                {
                    return;
                }
            }

            if (source.GZip == null)
            {
                source.GZip = new GZip();

                string strFilename = RomRootDir.Getfilename(source.GZipSha1);
                source.GZip.ReadGZip(strFilename, false);
            }

            Stream coms;

            source.GZip.GetRawStream(out coms);
            coms.Position += sourceStart;

            long actualLength = Math.Min(sourceLength, destinationLength);

            coms.Read(destination, (int)destinationStart, (int)actualLength);

            coms.Close();
        }
コード例 #2
0
        private static bool ScanAFile(string realFilename, Stream memzip, string displayFilename)
        {
            Stream fStream;

            if (string.IsNullOrEmpty(realFilename) && memzip != null)
            {
                fStream = memzip;
            }
            else
            {
                int errorCode = FileStream.OpenFileRead(realFilename, out fStream);
                if (errorCode != 0)
                {
                    return(false);
                }
            }

            bool           ret           = false;
            HeaderFileType foundFileType = FileHeaderReader.FileHeaderReader.GetType(fStream, out int offset);

            fStream.Position = 0;
            RvFile tFile = UnCompFiles.CheckSumRead(fStream, offset);

            tFile.AltType = foundFileType;


            if (foundFileType == HeaderFileType.CHD)
            {
                // read altheader values from CHD file.
            }

            // test if needed.
            FindStatus res = RvRomFileMatchup.FileneededTest(tFile);

            if (res == FindStatus.FileNeededInArchive)
            {
                _bgw?.ReportProgress(0, new bgwShowError(displayFilename, "found"));
                Debug.WriteLine("Reading file as " + tFile.SHA1);
                GZip   gz      = new GZip(tFile);
                string outfile = RomRootDir.Getfilename(tFile.SHA1);
                fStream.Position = 0;
                gz.WriteGZip(outfile, fStream, false);

                tFile.CompressedSize = gz.compressedSize;
                tFile.DBWrite();
                ret = true;
            }
            else if (res == FindStatus.FoundFileInArchive)
            {
                ret = true;
            }

            if (foundFileType == HeaderFileType.ZIP || foundFileType == HeaderFileType.SevenZip || foundFileType == HeaderFileType.GZ)
            {
                ICompress fz;
                switch (foundFileType)
                {
                case HeaderFileType.SevenZip:
                    fz = new SevenZ();
                    break;

                case HeaderFileType.GZ:
                    fz = new gZip();
                    break;

                //case HeaderFileType.ZIP:
                default:
                    fz = new Zip();
                    break;
                }

                fStream.Position = 0;

                ZipReturn zp;

                if (string.IsNullOrEmpty(realFilename) && memzip != null)
                {
                    zp = fz.ZipFileOpen(memzip);
                }
                else
                {
                    zp = fz.ZipFileOpen(realFilename);
                }

                if (zp == ZipReturn.ZipGood)
                {
                    bool allZipFound = true;
                    for (int i = 0; i < fz.LocalFilesCount(); i++)
                    {
                        ZipReturn openFile = fz.ZipFileOpenReadStream(i, out Stream stream, out ulong streamSize);

                        if (streamSize <= _inMemorySize)
                        {
                            if (openFile == ZipReturn.ZipTryingToAccessADirectory)
                            {
                                continue;
                            }
                            byte[] tmpFile = new byte[streamSize];
                            stream.Read(tmpFile, 0, (int)streamSize);
                            using (Stream memStream = new MemoryStream(tmpFile, false))
                            {
                                allZipFound &= ScanAFile(null, memStream, fz.Filename(i));
                            }
                        }
                        else
                        {
                            string file = Path.Combine(_tmpDir, Guid.NewGuid().ToString());
                            FileStream.OpenFileWrite(file, out Stream fs);
                            ulong sizetogo = streamSize;
                            while (sizetogo > 0)
                            {
                                int sizenow = sizetogo > (ulong)Buffersize ? Buffersize : (int)sizetogo;
                                stream.Read(Buffer, 0, sizenow);
                                fs.Write(Buffer, 0, sizenow);
                                sizetogo -= (ulong)sizenow;
                            }
                            fs.Close();

                            allZipFound &= ScanAFile(file, null, fz.Filename(i));

                            File.Delete(file);
                        }
                        //fz.ZipFileCloseReadStream();
                    }
                    fz.ZipFileClose();
                    ret |= allZipFound;
                }
                else
                {
                    ret = false;
                }
            }

            if (!string.IsNullOrEmpty(realFilename) || memzip == null)
            {
                fStream.Close();
                fStream.Dispose();
            }


            return(ret);
        }
コード例 #3
0
ファイル: ExtractFiles.cs プロジェクト: vidarw/RVWorld
        public static void extract(string dirName)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            DialogResult        result = folderBrowserDialog1.ShowDialog();

            if (result != DialogResult.OK)
            {
                return;
            }
            string outPath = folderBrowserDialog1.SelectedPath;

            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", Program.db.Connection);
            }
            CommandFindRomsInGame.Parameters.Add(new SQLiteParameter("GameId"));


            byte[] buff = new byte[1024];

            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 + "%'", Program.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);

                ZipFile memZip = new ZipFile();
                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)
                        {
                            ZipFile.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.ReadGZip(strFilename, false);
                        Stream oStr;
                        GZip.GetRawStream(out oStr);

                        ulong sizetogo = compressedSize;
                        while (sizetogo > 0)
                        {
                            ulong sizenow = sizetogo > 1024 ? 1024 : sizetogo;
                            oStr.Read(buff, 0, (int)sizenow);
                            _zipFs.Write(buff, 0, (int)sizenow);
                            sizetogo -= sizenow;
                        }
                        oStr.Dispose();
                        GZip.Close();

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

                        romCount += 1;
                    }
                }

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

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