Exemplo n.º 1
0
        public IEnumerable <PCKFileEntry> ReadFileV3(PCKStream stream)
        {
            stream.Seek(-8, SeekOrigin.End);
            int FilesCount = stream.ReadInt32();

            form.UpdateProgressBar(form.Controls["progBar"], "max", FilesCount);
            stream.Seek(-280, SeekOrigin.End);
            long FileTableOffset = stream.ReadInt64() ^ stream.key.KEY_1;

            stream.Seek(FileTableOffset, SeekOrigin.Begin);
            BinaryReader TableStream = new BinaryReader(new MemoryStream(stream.ReadBytes((int)(stream.GetLenght() - FileTableOffset - 288))));

            for (int i = 0; i < FilesCount; ++i)
            {
                int EntrySize = TableStream.ReadInt32() ^ stream.key.KEY_1;
                TableStream.ReadInt32();
                yield return(new PCKFileEntry(TableStream.ReadBytes(EntrySize), version));
            }
        }
Exemplo n.º 2
0
        public void _Compress(string dir)
        {
            string pck = dir.Replace(".files", "");

            if (File.Exists(pck))
            {
                File.Delete(pck);
            }
            if (File.Exists(pck.Replace(".pck", ".pkx")))
            {
                File.Delete(pck.Replace(".pck", ".pkx"));
            }
            form.UpdateProgress(form.Controls["lblProgress"], "Listing the files");
            string[]  files  = Directory.GetFiles(dir, "*", SearchOption.AllDirectories);
            PCKStream stream = new PCKStream(pck);

            stream.WriteInt32(stream.key.FSIG_1);
            stream.WriteInt32(0);
            stream.WriteInt32(stream.key.FSIG_2);
            form.UpdateProgressBar(form.Controls["progBar"], "max", files.Length);
            MemoryStream FileTable = new MemoryStream();

            events = new CountdownEvent(files.Length);
            ManualResetEvent[] mres = new ManualResetEvent[files.Length];
            for (int i = 0; i < files.Length; i++)
            {
                mres[i] = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(x => {
                    int pos             = (int)x;
                    string file         = files[pos].Replace(dir, "").Replace("/", "\\").Remove(0, 1);
                    byte[] decompressed = File.ReadAllBytes(Path.Combine(dir, files[pos]));
                    byte[] compressed   = PCKZlib.Compress(decompressed, compressionLevel);
                    var entry           = new PCKFileEntry()
                    {
                        Path           = file,
                        Offset         = (uint)stream.Position,
                        Size           = decompressed.Length,
                        CompressedSize = compressed.Length
                    };
                    byte[] buffer = entry.Write(compressionLevel);
                    lock (FileTable)
                    {
                        FileTable.Write(BitConverter.GetBytes(buffer.Length ^ stream.key.KEY_1), 0, 4);
                        FileTable.Write(BitConverter.GetBytes(buffer.Length ^ stream.key.KEY_2), 0, 4);
                        FileTable.Write(buffer, 0, buffer.Length);
                    }
                    if (pos > 0)
                    {
                        mres[pos - 1].WaitOne();
                    }
                    stream.WriteBytes(compressed);
                    form.UpdateProgressBar(form.Controls["progBar"], "value", pos);
                    form.UpdateProgress(form.Controls["lblProgress"], "Compressing " +
                                        ((ProgressBar)form.Controls["progBar"]).Value + "/" +
                                        ((ProgressBar)form.Controls["progBar"]).Maximum + " : " + file);
                    mres[pos].Set();
                    events.Signal();
                }, i);
            }
            form.UpdateProgress(form.Controls["lblProgress"], "Waiting Threads to Finish");
            events.Wait();
            long FileTableOffset = stream.Position;

            stream.WriteBytes(FileTable.ToArray());
            stream.WriteInt32(stream.key.ASIG_1);                                                  //4
            stream.WriteInt16(2);                                                                  //2
            stream.WriteInt16(2);                                                                  //2
            stream.WriteUInt32((uint)(FileTableOffset ^ stream.key.KEY_1));                        //4
            stream.WriteInt32(0);                                                                  //4
            stream.WriteBytes(Encoding.Default.GetBytes("Angelica File Package, Perfect World.")); //37
            byte[] nuller = new byte[215];
            stream.WriteBytes(nuller);                                                             //215 - 268
            stream.WriteInt32(stream.key.ASIG_2);                                                  //4
            stream.WriteInt32(files.Length);                                                       //4
            stream.WriteInt16(2);                                                                  //2
            stream.WriteInt16(2);                                                                  //2
            stream.Seek(4, SeekOrigin.Begin);
            stream.WriteUInt32((uint)stream.GetLenght());
            stream.Dispose();
            form.UpdateProgressBar(form.Controls["progBar"], "value", 0);
            form.UpdateProgress(form.Controls["lblProgress"], "Ready");
        }