Exemplo n.º 1
0
 public void deleteFile(PckEntry file)
 {
     if (table.ContainsKey(file.filePath))
     {
         table.Remove(file.filePath);
     }
 }
Exemplo n.º 2
0
 public void addFilesToTable(string fileName, string dropedFile, bool isUpdate)
 {
     if (isUpdate)
     {
         /*
          * PckEntry fte = new PckEntry();
          * MemoryStream fileMs = new MemoryStream(File.ReadAllBytes(dir + "\\" + files[a]));
          * fte.filePath = files[a];
          * fte.fileoffset = (uint)bw.BaseStream.Position;
          * fte.decompressedSize = (int)fileMs.Length;
          * MemoryStream ms = new MemoryStream();
          * CompressStream(fileMs, ms, (int)fileMs.Length);
          * byte[] buffer = ms.ToArray();
          * fte.compressedSize = buffer.Length;
          * bw.Write(buffer);
          * fileTable.Add(fte);
          */
         //update table file
         if (table.ContainsKey(fileName))
         {
             PckEntry     fte    = table[fileName];
             MemoryStream fileMs = new MemoryStream(File.ReadAllBytes(dropedFile));
             fte.filePath         = fte.filePath;
             fte.fileoffset       = 0; //TO DO WHEN SAVE
             fte.decompressedSize = (int)fileMs.Length;
             fte.memoryun         = fileMs;
             //fte.memoryun.Flush();
             MemoryStream ms = new MemoryStream();
             CompressStream(fileMs, ms, (int)fileMs.Length);
             byte[] buffer = ms.ToArray();
             fte.compressedSize = buffer.Length;
             fte.memory         = new MemoryStream(buffer);
             //fte.memory.Flush();
             table[fileName] = fte;
             directoryStructure.addfille(fte);
         }
     }
     else
     {
         //add file to table
         PckEntry     fte    = new PckEntry();
         MemoryStream fileMs = new MemoryStream(File.ReadAllBytes(dropedFile));
         fte.filePath         = fileName;
         fte.fileoffset       = 0; //TO DO WHEN SAVE
         fte.decompressedSize = (int)fileMs.Length;
         fte.memoryun         = fileMs;
         //fte.memoryun.Flush();
         MemoryStream ms = new MemoryStream();
         CompressStream(fileMs, ms, (int)fileMs.Length);
         byte[] buffer = ms.ToArray();
         fte.compressedSize = buffer.Length;
         fte.memory         = new MemoryStream(buffer);
         //fte.memory.Flush();
         table[fileName] = fte;
         directoryStructure.addfille(fte);
     }
 }
Exemplo n.º 3
0
 public void saveFile(string path, byte[] buffer, PckEntry fte, bool compresed)
 {
     if (compresed)
     {
         MemoryStream  ms  = new MemoryStream();
         ZOutputStream zos = new ZOutputStream(ms);
         CopyStream(new MemoryStream(buffer), zos, fte.compressedSize);
         File.WriteAllBytes(path, ms.ToArray());
     }
     else
     {
         File.WriteAllBytes(path, buffer);
     }
 }
Exemplo n.º 4
0
        public PackStructureManager readOnly(string filepath)
        {
            directoryStructure = new PackStructureManager(Path.GetFileNameWithoutExtension(filepath));
            progress_bar2("Reading ...", 0, 100);
            Application.DoEvents();
            bool compleate = Merge(filepath);

            if (compleate == true)
            {
                filePathToshow = filepath;
                BinaryReader br = GenNewbinaryReader(getRealPath());
                br.BaseStream.Seek(-8, SeekOrigin.End);
                int entryCount = br.ReadInt32();
                br.BaseStream.Seek(-272, SeekOrigin.End);
                fileTableOffset = (long)((ulong)((uint)(br.ReadUInt32() ^ (ulong)KEY_1)));
                br.BaseStream.Seek(fileTableOffset, SeekOrigin.Begin);
                table = new SortedDictionary <string, PckEntry>();
                for (int a = 0; a < entryCount; ++a)
                {
                    int entrySize = br.ReadInt32() ^ KEY_1;
                    entrySize = br.ReadInt32() ^ KEY_2;
                    byte[] buffer = new byte[entrySize];
                    buffer = br.ReadBytes(entrySize);
                    PckEntry entry = null;
                    if (entrySize < 276)
                    {
                        entry = readTableEntry(buffer, true);
                        table[entry.filePath] = entry;
                    }
                    else
                    {
                        entry = readTableEntry(buffer, false);
                        table[entry.filePath] = entry;
                    }
                    table[entry.filePath].packPath = filepath;
                    directoryStructure.addfille(table[entry.filePath]);
                }
                br.Close();
                pckfileOpened.Dispose();
                pckfileOpened = null;
                progress_bar2("Ready", 0, 0);
                Application.DoEvents();
                return(directoryStructure);
            }
            return(null);
        }
Exemplo n.º 5
0
        public byte[] writeTableEntry(PckEntry fte)
        {
            byte[]       buffer = new byte[276];
            MemoryStream msb    = new MemoryStream(buffer);
            BinaryWriter bw     = new BinaryWriter(msb);

            bw.Write(Encoding.GetEncoding("GB2312").GetBytes(fte.filePath.Replace("/", "\\")));
            bw.BaseStream.Seek(260, SeekOrigin.Begin);
            bw.Write(fte.fileoffset);
            bw.Write(fte.decompressedSize);
            bw.Write(fte.compressedSize);
            bw.Write(0);
            msb.Seek(0, SeekOrigin.Begin);
            MemoryStream ms = new MemoryStream();

            CompressStream(msb, ms, 276);
            return(ms.ToArray());
        }
Exemplo n.º 6
0
        public PckEntry readTableEntry(byte[] buffer, bool compressed)
        {
            PckEntry     fte = new PckEntry();
            MemoryStream ms  = new MemoryStream(buffer);

            if (compressed)
            {
                byte[]        buf = new byte[276];
                ZOutputStream zos = new ZOutputStream(new MemoryStream(buf));
                CopyStream(new MemoryStream(buffer), zos, 276);
                buffer = buf;
            }
            BinaryReader br = new BinaryReader(new MemoryStream(buffer));

            fte.filePath         = Encoding.GetEncoding("GB2312").GetString(br.ReadBytes(260)).Split(new string[] { "\0" }, StringSplitOptions.RemoveEmptyEntries)[0].Replace("/", "\\");
            fte.fullP            = string.Empty;
            fte.fileoffset       = br.ReadUInt32();
            fte.decompressedSize = br.ReadInt32();
            fte.compressedSize   = br.ReadInt32();
            return(fte);
        }
Exemplo n.º 7
0
        public byte[] getChunkraw(string path, PckEntry table)
        {
            string filepath = path;

            byte[] datax = null;
            if (binaryReaderchunk == null || binaryReaderchunk.BaseStream == null)
            {
                binaryReaderchunk = GenNewbinaryReader(getRealPath());
            }
            if (table.fileoffset <= 0)
            {
                MessageBox.Show("Bad file Address: " + table.fileoffset);
            }
            else
            {
                byte[] buffer = new byte[table.compressedSize];
                binaryReaderchunk.BaseStream.Seek(table.fileoffset, SeekOrigin.Begin);
                buffer = binaryReaderchunk.ReadBytes(table.compressedSize);
                datax  = buffer;
            }
            return(datax);
        }
Exemplo n.º 8
0
        public byte[] getChunk(string path, PckEntry table)
        {
            Byte[] datax = null;
            if (table.memoryun != null)
            {
                return(table.memoryun.ToArray());
            }
            string filepath = path;

            if (binaryReaderchunk == null || binaryReaderchunk.BaseStream == null)
            {
                binaryReaderchunk = GenNewbinaryReader(getRealPath());
            }
            if (table.fileoffset <= 0)
            {
                MessageBox.Show("Bad file Address: " + table.fileoffset);
            }
            else
            {
                byte[] buffer = new byte[table.compressedSize];
                binaryReaderchunk.BaseStream.Seek(table.fileoffset, SeekOrigin.Begin);
                buffer = binaryReaderchunk.ReadBytes(table.compressedSize);
                if (table.compressedSize < table.decompressedSize)
                {
                    MemoryStream  ms  = new MemoryStream();
                    ZOutputStream zos = new ZOutputStream(ms);
                    CopyStream(new MemoryStream(buffer), zos, table.compressedSize);
                    datax = ms.ToArray();
                }
                else
                {
                    datax = buffer;
                }
            }
            return(datax);
        }
Exemplo n.º 9
0
        public void pack(string dir)
        {
            if (!Directory.Exists(dir))
            {
                unpack();
            }
            progress_bar2("Packing ...", 0, 100);
            string pck = dir.Replace(".files\\", "").Replace(".files", "");

            string[] files = Directory.GetFiles(dir, "*", SearchOption.AllDirectories);
            for (int a = 0; a < files.Length; ++a)
            {
                files[a] = files[a].Replace(dir, "").Replace("/", "\\").Remove(0, 1);
            }
            fileTableOffset = 0;
            List <PckEntry> fileTable = new List <PckEntry>();
            BinaryWriter    bw        = new BinaryWriter(new FileStream(pck, FileMode.Create, FileAccess.ReadWrite));

            bw.Write(FSIG_1);
            bw.Write(0);
            bw.Write(FSIG_2);
            int show = 0;

            for (int a = 0; a < files.Length; ++a)
            {
                PckEntry     fte    = new PckEntry();
                MemoryStream fileMs = new MemoryStream(File.ReadAllBytes(dir + "\\" + files[a]));
                fte.filePath         = files[a];
                fte.fileoffset       = (uint)bw.BaseStream.Position;
                fte.decompressedSize = (int)fileMs.Length;
                MemoryStream ms = new MemoryStream();
                CompressStream(fileMs, ms, (int)fileMs.Length);
                byte[] buffer = ms.ToArray();
                fte.compressedSize = buffer.Length;
                bw.Write(buffer);
                fileTable.Add(fte);
                if (show <= 0)
                {
                    Application.DoEvents();
                    progress_bar2(string.Format("Writeing records: {0}/{1}", a, files.Length), a, files.Length);
                    show = showMax;
                }
                else
                {
                    show--;
                }
            }
            fileTableOffset = bw.BaseStream.Position;
            for (int a = 0; a < fileTable.Count; ++a)
            {
                byte[] buffer = writeTableEntry(fileTable[a]);
                bw.Write(buffer.Length ^ KEY_1);
                bw.Write(buffer.Length ^ KEY_2);
                bw.Write(buffer);
                if (show <= 0)
                {
                    Application.DoEvents();
                    progress_bar2(string.Format("Writeing files: {0}/{1}", a, files.Length), a, files.Length);
                    show = showMax;
                }
                else
                {
                    show--;
                }
            }
            bw.Write(ASIG_1);
            bw.Write((short)2);
            bw.Write((short)2);
            bw.Write((uint)(fileTableOffset ^ KEY_1));
            bw.Write(0);
            bw.Write(Encoding.Default.GetBytes("Angelica File Package, Perfect World."));
            byte[] nuller = new byte[215];
            bw.Write(nuller);
            bw.Write(ASIG_2);
            bw.Write(fileTable.Count);
            bw.Write((short)2);
            bw.Write((short)2);
            bw.BaseStream.Seek(4, SeekOrigin.Begin);
            bw.Write((uint)bw.BaseStream.Length);
            bw.Close();
            Split(pck);
            progress_bar2("Ready", 0, 0);
        }
Exemplo n.º 10
0
        public void unpack()
        {
            progress_bar2("Unpacking ...", 0, 100);
            string path = filePathToshow + ".files\\";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            else
            {
                Directory.Delete(path, true);
            }
            //BinaryReader br = new BinaryReader(new FileStream(filePathToshow, FileMode.Open, FileAccess.Read));
            bool compleate = Merge(filePathToshow);

            if (compleate == true)
            {
                BinaryReader br = GenNewbinaryReader(getRealPath());
                br.BaseStream.Seek(-8, SeekOrigin.End);
                int entryCount = br.ReadInt32();
                br.BaseStream.Seek(-272, SeekOrigin.End);
                fileTableOffset = (long)((ulong)((uint)(br.ReadUInt32() ^ (ulong)KEY_1)));

                br.BaseStream.Seek(fileTableOffset, SeekOrigin.Begin);
                table = new SortedDictionary <string, PckEntry>();
                int show = 0;
                for (int a = 0; a < entryCount; ++a)
                {
                    int entrySize = br.ReadInt32() ^ KEY_1;
                    entrySize = br.ReadInt32() ^ KEY_2;
                    byte[] buffer = new byte[entrySize];
                    buffer = br.ReadBytes(entrySize);
                    PckEntry entry = null;
                    if (entrySize < 276)
                    {
                        entry = readTableEntry(buffer, true);
                        table[entry.filePath] = entry;
                    }
                    else
                    {
                        entry = readTableEntry(buffer, false);
                        table[entry.filePath] = entry;
                    }
                    if (show <= 0)
                    {
                        Application.DoEvents();
                        progress_bar2(string.Format("Reading tables: {0}/{1}", a, entryCount), a, entryCount);
                        show = showMax;
                    }
                    else
                    {
                        show--;
                    }
                }
                show = 0;
                int b = 0;
                foreach (PckEntry fte in table.Values)
                {
                    if (fte.fileoffset <= 0)
                    {
                        MessageBox.Show("Bad file Address: " + fte.fileoffset);
                    }
                    else
                    {
                        CreateDir(path, fte.filePath);
                        byte[] buffer = new byte[fte.compressedSize];
                        fte.fullP = path + fte.filePath;
                        // BinaryWriter bw = new BinaryWriter(new FileStream(fte.fullP, FileMode.Create, FileAccess.Write));
                        br.BaseStream.Seek(fte.fileoffset, SeekOrigin.Begin);
                        buffer = br.ReadBytes(fte.compressedSize);
                        if (fte.compressedSize < fte.decompressedSize)
                        {
                            Thread t = new Thread(new ThreadStart(() => { saveFile(fte.fullP, buffer, fte, true); }));
                            t.Start();
                        }
                        else
                        {
                            Thread t = new Thread(new ThreadStart(() => { saveFile(fte.fullP, buffer, fte, false); }));
                            t.Start();
                        }
                        //bw.Close();
                    }
                    if (show <= 0)
                    {
                        Application.DoEvents();
                        progress_bar2(string.Format("Unpacking: {0}/{1}", b, entryCount), b, entryCount);
                        show = showMax;
                    }
                    else
                    {
                        show--;
                    }
                    b++;
                }
                br.Close();
                br.Close();
                pckfileOpened.Dispose();
                pckfileOpened = null;
                if (filePathToshow.EndsWith("x"))
                {
                    File.Delete(filePathToshow);
                }
            }
            progress_bar2("Ready", 0, 0);
        }