Beispiel #1
0
        private static void Qcreatezip(bool ignoreFiles = false)
        {
            List <string> files = new List <string>();

            if (ignoreFiles == false)
            {
                files.AddRange(from x in inputArray("Input Files") select x.Trim('\"'));
            }
            var dir = input("Root Dir: ");

            if (dir != "")
            {
                dir = new DirectoryInfo(dir).FullName;
            }
            if (files.Count < 1)
            {
                files.AddRange(Directory.GetFiles(dir, "*", SearchOption.AllDirectories));
            }
            var output = input("Output File: ");
            var fs     = File.Open(output, FileMode.Create, FileAccess.ReadWrite);

            NZ.Create(fs, files.ToArray(), dir, Out);
            fs.Flush();
            fs.Close();
        }
Beispiel #2
0
        private static void Qunzip()
        {
            var file = input("NZip File: ");
            var dir  = input("Output Dir: ").TrimEnd('\\', '/');
            var fs   = File.OpenRead(file);
            var fsz  = NZ.FromStream(fs);

            WriteLine("[Files:]");
            foreach (var item in fsz.GetFiles())
            {
                WriteLine(item.name);
                var outputpath = $"{dir}\\{item.name}";
                var fi         = new FileInfo(outputpath);
                if (Directory.Exists(Path.GetDirectoryName(outputpath)) == false)
                {
                    Directory.CreateDirectory(fi.DirectoryName);
                }
                using (var ofs = File.Open(outputpath, FileMode.Create, FileAccess.ReadWrite)) {
                    ofs.SetLength(item.length);
                    fsz.WriteFileTo(item, ofs);
                }
                if (item.lwt > 0)
                {
                    fi.LastWriteTime = item.GetLastWriteTime();
                }
            }
            fs.Close();
        }
Beispiel #3
0
        public static NZ GetPack()
        {
            var offset = GetPackOffset();
            var fsz    = NZ.FromStream(File.Open(Magic.GetSelfPath(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite), offset);

            return(fsz);
        }
Beispiel #4
0
        public static void RunAttachedExe(string[] args)
        {
            if (!IsExeAttached)
            {
                throw new Exception("exe is not attached.");
            }

            NZ fsz = GetPack();

            RegisterAssemblyResolver(fsz);

            var exeBytes    = fsz.GetFileBytes(ExeName);
            var exeAssembly = Assembly.Load(exeBytes);
            var entryPoint  = exeAssembly.EntryPoint;

            var flags         = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
            var isPackedField = entryPoint.DeclaringType.GetField("__magic_is_packed", flags);
            var pkgField      = entryPoint.DeclaringType.GetField("__magic_files", flags);

            if (isPackedField != null && isPackedField.FieldType == typeof(bool))
            {
                isPackedField.SetValue(null, true);
            }
            if (pkgField != null && pkgField.FieldType == typeof(IDictionary <string, Func <byte[]> >))
            {
                var fszDict = new Dictionary <string, Func <byte[]> >();
                foreach (var item in fsz.GetFiles())
                {
                    fszDict.Add(item.name, () => fsz.GetFileBytes(item));
                }
                pkgField.SetValue(null, fszDict);
            }

            entryPoint.Invoke(null, new object[] { args });
        }
Beispiel #5
0
        public static NZ FromStream(Stream stream, int posOffset)
        {
            var ret = new NZ();

            ret.posOffset = posOffset;
            ret.stream    = stream;
            ret.init();
            return(ret);
        }
Beispiel #6
0
        public static void LoadAttachedDlls()
        {
            if (!IsDllsAttached)
            {
                throw new Exception("DLLs are not attached.");
            }
            NZ fsz = GetPack();

            RegisterAssemblyResolver(fsz);
        }
Beispiel #7
0
        private static void RegisterAssemblyResolver(NZ fsz)
        {
            var asms = new Dictionary <string, NZFileinfo>();

            foreach (var item in fsz.GetFiles())
            {
                if (item.name != ExeName && item.name.StartsWith(NamePrefix))
                {
                    asms.Add(item.name.Substring(NamePrefix.Length), item);
                }
            }
            AppDomain.CurrentDomain.AssemblyResolve += (s, arg) => {
                if (!asms.TryGetValue(arg.Name, out var nfi))
                {
                    //Console.Error.WriteLine($"NZip: assembly '{arg.Name}' not found");
                    return(null);
                }
                return(Assembly.Load(fsz.GetFileBytes(nfi)));
            };
        }
Beispiel #8
0
        private static void Qlszip()
        {
            var file = input("NZip to list: ");
            var fs   = File.OpenRead(file);
            var fsz  = NZ.FromStream(fs);

            if (fsz.CreateTime != DateTime.MinValue)
            {
                WriteLine($"CreateTime: {fsz.CreateTime}");
            }
            WriteLine("[Files:]");
            WriteLine("Size\tGZSize\tName");
            var  files     = fsz.GetFiles();
            long sizesum   = 0;
            long ziplensum = 0;

            foreach (var item in files)
            {
                WriteLine($"{item.length}\t{item.ziplen}\t{item.name}");
                sizesum   += item.length;
                ziplensum += item.ziplen;
            }
            WriteLine($"[Total {files.Length} files, {ziplensum}/{sizesum} bytes ({Convert.ToInt32((double)ziplensum / sizesum * 100)} %)]");
        }
Beispiel #9
0
 public DateTime GetLastWriteTime() => NZ.ConvertIntDateTime(lwt);
Beispiel #10
0
 public static void Pack(string exe, List <string> dlls, string outputPath, Magic.Dict addDict = null, bool?setGui = null)
 {
     using (var fs = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) {
         bool   extExe = exe != null;
         var    list   = new List <AddingFile>();
         byte[] exebuf;
         if (extExe)
         {
             exebuf = File.ReadAllBytes(exe);
             setGui = setGui ?? Magic.getSubsystem(exebuf) == 2;
             list.Add(new AddingFile(exe, ExeName));
         }
         int selfExeLength = GetSelfExeLength();
         var dict          = Magic.getMagicDict();
         if (addDict != null)
         {
             foreach (var item in addDict)
             {
                 if (item.Value == null)
                 {
                     dict.Remove(item.Key);
                 }
                 else
                 {
                     dict[item.Key] = item.Value;
                 }
             }
         }
         dict["pack"] = selfExeLength.ToString();
         char magicCh    = extExe ? 'm' : (dlls != null) ? 'd' : Magic.magic_char;
         var  selfexebuf = Magic.genExe(magicCh, setGui, dict.ToString(), selfExeLength);
         fs.Write(selfexebuf, 0, selfexebuf.Length);
         int i = 0;
         if (dlls != null)
         {
             foreach (var x in dlls)
             {
                 i++;
                 if (File.Exists(x))
                 {
                     var name = AssemblyName.GetAssemblyName(x).FullName;
                     list.Add(new AddingFile(x, NamePrefix + name));
                 }
                 else
                 {
                     throw new Exception("File not found: " + x);
                 }
             }
         }
         var curMagicCh = Magic.magic_char;
         if (exe != null || dlls != null)
         {
             NZ.Create(fs, list, Console.Out);
         }
         else
         {
             using (var self = File.Open(Magic.GetSelfPath(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                 self.Position = selfExeLength;
                 self.CopyTo(fs, 64 * 1024);
             }
         }
     }
 }