Exemple #1
0
        public int Packing()
        {
            Print("ファイル構成開始");
            string fileName = string.IsNullOrEmpty(ipfTgtName) ? "_p" + DateTime.Now.ToString("yyMMddhhmmss") : ipfTgtName;

            using (FileStream fw = new FileStream(Directory.GetCurrentDirectory() + "/" + fileName + ".ipf", FileMode.Create, FileAccess.ReadWrite)) {
                foreach (var fti in lstFileTab)
                {
                    using (FileStream fr = new FileStream(fti.filePath, FileMode.Open, FileAccess.Read)) {
                        IpfCrypt ic = new IpfCrypt();
                        fti.dataPos = (int)fw.Position;
                        fti.deplLen = (int)fr.Length;
                        string ext = Path.GetExtension(fti.filePath.ToLower());
                        if ((ext == ".jpg") || (ext == ".fsb") || (ext == ".mp3"))
                        {
                            byte[] readBuf = new byte[4096];
                            for (int readSize = 0; ;)
                            {
                                readSize = fr.Read(readBuf, 0, readBuf.Length);
                                if (readSize == 0)
                                {
                                    break;
                                }
                                fw.Write(readBuf, 0, readSize);
                            }
                        }
                        else
                        {
                            using (var memStrm = new MemoryStream()) {
                                using (var deflStrm = new DeflateStream(memStrm, CompressionMode.Compress, true)) {
                                    byte[] readBuf = new byte[4096];
                                    for (int readSize = 0, readPos = 0; ; readPos += readSize)
                                    {
                                        Array.Resize(ref readBuf, readPos + 4096);
                                        readSize = fr.Read(readBuf, readPos, 4096);
                                        if (readSize == 0)
                                        {
                                            Array.Resize(ref readBuf, readPos);
                                            break;
                                        }
                                    }
                                    deflStrm.Write(readBuf, 0, readBuf.Length);
                                }
                                var tmpBuf = memStrm.ToArray();
                                //DumpBuf(tmpBuf, 32);
                                ic.EncryptBuf(tmpBuf);
                                //DumpBuf(tmpBuf, 32);
                                fw.Write(tmpBuf, 0, tmpBuf.Length);
                            }
                        }
                        fti.compLen = (int)fw.Position - fti.dataPos;
                        fr.Seek(0, SeekOrigin.Begin);
                        fti.fileCrc = ic.ComputeHash(fr);
                    }
                }
                ipfFileCnt    = lstFileTab.Count;
                ipfFileTblPos = (int)fw.Position;
                int lastDataPos = 0;
                foreach (var fti in lstFileTab)
                {
                    byte[] archNmB = Encoding.UTF8.GetBytes(fti.archNm);
                    byte[] fileNmB = Encoding.UTF8.GetBytes(fti.fileNm);
                    byte[] tmpBuf  = new byte[20];
                    tmpBuf[0]   = (byte)(fileNmB.Length); tmpBuf[1] = (byte)(fileNmB.Length >> 8);
                    tmpBuf[18]  = (byte)(archNmB.Length); tmpBuf[19] = (byte)(archNmB.Length >> 8);
                    tmpBuf[2]   = (byte)(fti.fileCrc); tmpBuf[3] = (byte)(fti.fileCrc >> 8); tmpBuf[4] = (byte)(fti.fileCrc >> 16); tmpBuf[5] = (byte)(fti.fileCrc >> 24);
                    tmpBuf[6]   = (byte)(fti.compLen); tmpBuf[7] = (byte)(fti.compLen >> 8); tmpBuf[8] = (byte)(fti.compLen >> 16); tmpBuf[9] = (byte)(fti.compLen >> 24);
                    tmpBuf[10]  = (byte)(fti.deplLen); tmpBuf[11] = (byte)(fti.deplLen >> 8); tmpBuf[12] = (byte)(fti.deplLen >> 16); tmpBuf[13] = (byte)(fti.deplLen >> 24);
                    tmpBuf[14]  = (byte)(fti.dataPos); tmpBuf[15] = (byte)(fti.dataPos >> 8); tmpBuf[16] = (byte)(fti.dataPos >> 16); tmpBuf[17] = (byte)(fti.dataPos >> 24);
                    lastDataPos = fti.dataPos;

                    fw.Write(tmpBuf, 0, tmpBuf.Length);
                    fw.Write(archNmB, 0, archNmB.Length);
                    fw.Write(fileNmB, 0, fileNmB.Length);
                }
                ipfFileFtrPos = (int)lastDataPos;
                {
                    byte[] tmpBuf = new byte[24];
                    tmpBuf[0]  = (byte)(ipfFileCnt); tmpBuf[1] = (byte)(ipfFileCnt >> 8);
                    tmpBuf[2]  = (byte)(ipfFileTblPos); tmpBuf[3] = (byte)(ipfFileTblPos >> 8); tmpBuf[4] = (byte)(ipfFileTblPos >> 16); tmpBuf[5] = (byte)(ipfFileTblPos >> 24);
                    tmpBuf[8]  = (byte)(ipfFileFtrPos); tmpBuf[9] = (byte)(ipfFileFtrPos >> 8); tmpBuf[10] = (byte)(ipfFileFtrPos >> 16); tmpBuf[11] = (byte)(ipfFileFtrPos >> 24);
                    tmpBuf[12] = 0x50; tmpBuf[13] = 0x4B; tmpBuf[14] = 0x05; tmpBuf[15] = 0x06;
                    tmpBuf[16] = (byte)(ipfTgtVer); tmpBuf[17] = (byte)(ipfTgtVer >> 8); tmpBuf[18] = (byte)(ipfTgtVer >> 16); tmpBuf[19] = (byte)(ipfTgtVer >> 24);
                    tmpBuf[20] = (byte)(ipfPkgVer); tmpBuf[21] = (byte)(ipfPkgVer >> 8); tmpBuf[22] = (byte)(ipfPkgVer >> 16); tmpBuf[23] = (byte)(ipfPkgVer >> 24);
                    fw.Write(tmpBuf, 0, tmpBuf.Length);
                }
            }
            Print("ファイル構成完了");
            Print("----");
            return(0);
        }