Example #1
0
        static void Main(string[] args)
        {
            ArgMan.Instance.ParseArgs(args);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (ArgMan.Instance.CodeConversionOnly && ArgMan.Instance.Files != null)
            {
                foreach (string filePath in ArgMan.Instance.Files)
                {
                    if (File.Exists(filePath))
                    {
                        string backupFilePath = String.Format("{0}.bak", filePath);
                        //Cleanup existing backup file.
                        if (File.Exists(backupFilePath))
                        {
                            File.Delete(backupFilePath);
                        }
                        File.Move(filePath, backupFilePath);

                        //If no conversion happened, restore the file.
                        Util.ConversionResult ret = Util.ConvertChsToCht(backupFilePath, filePath, false);
                        if (ret == Util.ConversionResult.NoConversion ||
                            ret == Util.ConversionResult.Error)
                        {
                            if (File.Exists(filePath))
                            {
                                File.Delete(filePath);
                            }

                            File.Move(backupFilePath, filePath);
                        }
                    }
                }

                MessageBox.Show(Properties.Resources.InfoEasyConversionOk, Properties.Resources.InfoTitle);
            }
            else
            {
                Application.Run(new DownloadForm());
            }
        }
Example #2
0
        private void HandleSingleSub(BinaryReader reader)
        {
            //TODO: Handle errors
            int singleFilePackLen = Util.BytesToInt32(reader.ReadBytes(4), ByteOrder.BigEndian);
            int extLen            = Util.BytesToInt32(reader.ReadBytes(4), ByteOrder.BigEndian);

            byte[] ext       = reader.ReadBytes(extLen);
            string extString = Encoding.UTF8.GetString(ext);
            int    fileLen   = Util.BytesToInt32(reader.ReadBytes(4), ByteOrder.BigEndian);

            int       leftToRead = fileLen;
            const int BufferSize = 4096;

            byte[]     buffer       = new byte[BufferSize];
            string     tempFilePath = Path.GetTempFileName();
            FileStream tempStream   =
                new FileStream(tempFilePath, FileMode.Open, FileAccess.ReadWrite);
            bool bGzipped = false;

            do
            {
                int needToRead = Math.Min(leftToRead, BufferSize);
                int accuRead   = reader.Read(buffer, 0, needToRead);

                if (leftToRead == fileLen) //if it's the first round.
                {
                    //check if the data is gzipped.
                    bGzipped = (buffer[0] == 0x1f) && (buffer[1] == 0x8b) && (buffer[2] == 0x08);
                }

                if (accuRead == 0)
                {
                    break;
                }
                leftToRead -= accuRead;

                //Output
                tempStream.Write(buffer, 0, accuRead);
            } while (leftToRead > 0);
            tempStream.Close();

            bool bOk = true;

            //decompress
            if (bGzipped)
            {
                string flatTempFilePath = Path.GetTempFileName();
                bool   ret = Util.UnGZip(tempFilePath, flatTempFilePath);
                File.Delete(tempFilePath);
                if (!ret)
                {
                    LogMan.Instance.Log(Resources.ErrUnGZipFailed, _videoFileName);
                    File.Delete(flatTempFilePath);
                    bOk = false;
                }
                else
                {
                    //continue processing the decompressed file.
                    tempFilePath = flatTempFilePath;
                }
            }

            if (bOk)
            {
                if (Settings.Default.AutoChsToChtConversion == true)
                {
                    //Do converision
                    string chtTempPath = Path.GetTempFileName();
                    LogMan.Instance.Log(Resources.InfoStartChtConversion, _videoFileName);
                    Util.ConversionResult ret = Util.ConvertChsToCht(tempFilePath, chtTempPath);

                    if (ret == Util.ConversionResult.OK)
                    {
                        LogMan.Instance.Log(Resources.InfoChtConversionOk, _videoFileName);
                        File.Delete(tempFilePath);
                        //continue processing the decompressed file.
                        tempFilePath = chtTempPath;
                    }
                    else if (ret == Util.ConversionResult.NoConversion)
                    {
                        //No conversion happened
                        LogMan.Instance.Log(Resources.InfoNoConversion, _videoFileName);
                        File.Delete(chtTempPath);
                    }
                    else
                    {
                        LogMan.Instance.Log(Resources.ErrChtConvertion, _videoFileName);
                        File.Delete(chtTempPath);
                    }
                }
            }

            if (bOk)
            {
                //final output
                File.Move(tempFilePath, GetOutputFilename(extString));
            }
        }