Example #1
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);
     }
 }
Example #2
0
        private void ReadTable(string path)
        {
            psm     = new PackStructureManager(Path.GetFileNameWithoutExtension(path));
            streams = new List <Stream>();
            if (File.Exists(path))
            {
                streams.Add(new FileStream(path, FileMode.Open, FileAccess.Read));
            }
            string pkx = path.Replace(".pck", ".pkx");

            if (File.Exists(pkx))
            {
                streams.Add(new FileStream(pkx, FileMode.Open, FileAccess.Read));
            }
            stream = new CombinationStream(streams);

            BinaryReader br = new BinaryReader(stream);

            br.BaseStream.Seek(-8, SeekOrigin.End);
            int entryCount = br.ReadInt32();

            br.BaseStream.Seek(-272, SeekOrigin.End);
            long fileTableOffset = (long)((ulong)((uint)(br.ReadUInt32() ^ (ulong)KEY_1)));

            br.BaseStream.Seek(fileTableOffset, SeekOrigin.Begin);
            PckEntry[] table = new PckEntry[entryCount];
            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);
                if (entrySize < 276)
                {
                    table[a] = readTableEntry(buffer, true);
                }
                else
                {
                    table[a] = readTableEntry(buffer, false);
                }
                string name = table[a].filePath.ToLower();
                files[name] = table[a];
                psm.addfille(files[name]);
            }

            initiated = true;
        }
Example #3
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);
        }
Example #4
0
        public int ReWrite()
        {
            string       pathx  = path + ".files\\";
            DialogResult result = DialogResult.No;

            if (Directory.Exists(pathx))
            {
                result = MessageBox.Show("Found files on disk do you want to rewrite from files?", "Question", MessageBoxButtons.YesNo);
            }
            #region AAAAA
            if (result == DialogResult.No)
            {
                LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(Environment.ProcessorCount / 2);
                List <Task>             tasks             = new List <Task>();
                TaskFactory             factory           = new TaskFactory(lcts);
                CancellationTokenSource cts = new CancellationTokenSource();
                MakeStreams();
                if (path != null && path.Length > 0)
                {
                    PckEntry[] values = table.Values.ToArray();
                    int        count  = values.Length;
                    for (int i = 0; i < values.Length; i++)
                    {
                        if (values[i].bytesCompresed == null)
                        {
                            PckEntry pck  = values[i];
                            byte[]   data = getChunk2("", pck);
                            progress_bar("Reading files ....", i, count);
                            Task t1 = factory.StartNew((o) =>
                            {
                                progress_bar("Compressing files ....", (int)o, count);
                                pck.bytesCompresed = PCKZlib.Compress(data, CompressionLevel);
                            }, i, cts.Token);
                            tasks.Add(t1);
                        }
                    }
                    progress_bar("Please Wait", 0, 0);
                    Task.WaitAll(tasks.ToArray());
                    Thread.Sleep(500);
                    cts.Dispose();
                    initialCount = count;
                    this.Dispose();
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    if (File.Exists(path.Replace(".pck", ".pkx")))
                    {
                        File.Delete(path.Replace(".pck", ".pkx"));
                    }

                    PCKStream stream = new PCKStream(path);
                    stream.WriteInt32(stream.key.FSIG_1);
                    stream.WriteInt32(0);
                    stream.WriteInt32(stream.key.FSIG_2);
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte = values[i];
                        fte.setVersion(version);

                        progress_bar("Writing files ....", i, count);
                        fte.fileoffset = (uint)stream.Position;
                        stream.WriteBytes(fte.bytesCompresed);
                    }
                    uint         fileTableOffset_tmp = (uint)stream.Position;
                    MemoryStream FileTable           = new MemoryStream();
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte = values[i];
                        fte.setVersion(version);
                        byte[] buffer = fte.Write(CompressionLevel);
                        lock (FileTable)
                        {
                            FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_1), 0, 4);
                            FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_2), 0, 4);
                            FileTable.Write(buffer, 0, buffer.Length);
                        }
                        progress_bar("Writing table...", i, count);
                    }
                    stream.WriteBytes(FileTable.ToArray());
                    FileTable.Flush();
                    FileTable.Dispose();
                    stream.WriteInt32(ASIG_1);
                    stream.WriteInt16(version);
                    stream.WriteInt16(2);
                    stream.WriteUInt32((uint)(fileTableOffset_tmp ^ KEY_1));
                    if (version > 2)
                    {
                        stream.WriteInt32(-1);
                    }
                    stream.WriteInt32(0);
                    NameChar namechar = new NameChar(252);
                    namechar.DefaultEncoding = Encoding.Default;
                    namechar.Name            = "Angelica File Package, writed by JHS Software.";
                    stream.WriteBytes(namechar.EncodedName);
                    stream.WriteInt32(ASIG_2);
                    if (version > 2)
                    {
                        stream.WriteInt32(512);
                    }
                    stream.WriteInt32(table.Count);
                    stream.WriteInt16(version);
                    stream.WriteInt16(2);
                    stream.Seek(4, SeekOrigin.Begin);
                    stream.WriteUInt32((uint)stream.GetLenght());
                    stream.Dispose();
                    progress_bar("Clearing memory...", 0, 0);
                    for (int i = 0; i < values.Length; i++)
                    {
                        PckEntry fte = values[i];
                        table[fte.id].bytes          = null;
                        table[fte.id].bytesCompresed = null;
                        table[fte.id].memory         = null;
                        progress_bar("Clearing memory...", i, count);
                    }
                    GC.Collect();
                    progress_bar("Ready", 0, 0);
                }
            }
            else
            #endregion
            #region FROM DISK
            {
                directoryStructure = null;
                GC.Collect();
                modified           = false;
                directoryStructure = new PackStructureManager(Path.GetFileNameWithoutExtension(path));
                table = new SortedList <int, PckEntry>();
                string[] files = Directory.GetFiles(pathx, "*", SearchOption.AllDirectories);

                int count = files.Length;
                initialCount = count;
                this.Dispose();
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                if (File.Exists(path.Replace(".pck", ".pkx")))
                {
                    File.Delete(path.Replace(".pck", ".pkx"));
                }

                PCKStream stream = new PCKStream(path);
                stream.WriteInt32(stream.key.FSIG_1);
                stream.WriteInt32(0);
                stream.WriteInt32(stream.key.FSIG_2);
                for (int i = 0; i < files.Length; i++)
                {
                    string   file  = files[i].Replace(pathx, "").Replace("/", "\\");
                    string   pathr = Path.Combine(pathx, file);
                    PckEntry data  = new PckEntry(version);
                    data.filePath = file;
                    byte[] datab          = File.ReadAllBytes(pathr);
                    byte[] bytesCompresed = PCKZlib.Compress(datab, CompressionLevel);
                    data.compressedSize   = bytesCompresed.Length;
                    data.decompressedSize = datab.Length;
                    data.id = i;
                    progress_bar("Writing files ....", i, count);
                    data.fileoffset = (uint)stream.Position;
                    stream.WriteBytes(bytesCompresed);
                    table[i] = data;
                    directoryStructure.addfille(table[i]);
                }
                progress_bar("Please Wait", 0, 0);
                Application.DoEvents();
                uint         fileTableOffset_tmp = (uint)stream.Position;
                MemoryStream FileTable           = new MemoryStream();
                progress_bar("Finalizing Pack...", 0, 0);
                for (int i = 0; i < table.Count; i++)
                {
                    PckEntry fte    = table[i];
                    byte[]   buffer = fte.Write(CompressionLevel);
                    lock (FileTable)
                    {
                        FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_1), 0, 4);
                        FileTable.Write(BitConverter.GetBytes(buffer.Length ^ KEY_2), 0, 4);
                        FileTable.Write(buffer, 0, buffer.Length);
                    }
                    progress_bar("Finalizing Pack...", i, count);
                }
                stream.WriteBytes(FileTable.ToArray());
                FileTable.Flush();
                FileTable.Dispose();
                stream.WriteInt32(ASIG_1);
                stream.WriteInt16(version);
                stream.WriteInt16(2);
                stream.WriteUInt32((uint)(fileTableOffset_tmp ^ KEY_1));
                if (version > 2)
                {
                    stream.WriteInt32(-1);
                }
                stream.WriteInt32(0);
                NameChar namechar = new NameChar(252);
                namechar.DefaultEncoding = Encoding.Default;
                namechar.Name            = "Angelica File Package, writed by JHS Software.";
                stream.WriteBytes(namechar.EncodedName);
                stream.WriteInt32(ASIG_2);
                if (version > 2)
                {
                    stream.WriteInt32(512);
                }
                stream.WriteInt32(table.Count);
                stream.WriteInt16(version);
                stream.WriteInt16(2);
                stream.Seek(4, SeekOrigin.Begin);
                stream.WriteUInt32((uint)stream.GetLenght());
                stream.Dispose();
                progress_bar("Clearing memory...", 0, 0);
                for (int i = 0; i < table.Count; i++)
                {
                    table[i].bytes          = null;
                    table[i].bytesCompresed = null;
                    table[i].memory         = null;
                    progress_bar("Clearing memory...", i, count);
                }
                GC.Collect();
                progress_bar("Ready", 0, 0);
            }
            #endregion
            return(0);//SUccess
        }