Example #1
0
        static void Main(string[] args)
        {
            WriteInfo("".PadRight(79, '*'));
            WriteInfo("ezPacker, Copyright (C) 2016 Sascha-Christian Hensel");
            WriteInfo("ezPacker comes with ABSOLUTELY NO WARRANTY.");
            WriteInfo("This is free software, and you are welcome to redistribute it");
            WriteInfo("under certain conditions; see LICENSE.");
            WriteInfo("".PadRight(79, '*'));
            WriteInfo("");

            if (args.Length != 1)
            {
                WriteError("No configuration file given!");
            }
            else
            {
                FileInfo projectFile = new FileInfo(args[0]);

                WriteInfo("Trying to load configuration file '{0}'...", projectFile);

                IProject project = null;

                IProjectContext projectContext = new DefaultProjectContext(projectFile);

                IProjectParser parser = new XmlProjectParser();
                using (FileStream stream = projectFile.OpenRead())
                {
                    project = parser.Parse(projectContext, stream);
                }

                IMatcherContext context = new MatcherContext()
                {
                    Project = project
                };

                IFileCollector collector = new DefaultFileCollector();
                foreach (FileInfo item in Matcher.GetMatches(context))
                {
                    collector.Add(item);
                }

                WriteInfo("Success! Found  '{0}' possible file(s) to include for packing.", collector.Count);

                if (project.Replacements.Count == 0)
                {
                    WriteInfo("No files need to replaced.");
                }
                else
                {
                    WriteInfo("Trying to replace '{0}' file(s)...", project.Replacements.Count);

                    foreach (Replacement replacement in project.Replacements)
                    {
                        WriteInfo("  Attempt to replace file '{0}'...", replacement.FileName);
                        if (!replacement.Exists())
                        {
                            WriteWarning("  ... failed: replacement file '{0}' does not exist!", replacement.ReplacementFile.FullName);
                            continue;
                        }

                        bool replaced = collector.Replace(context, replacement.FileName, replacement.ReplacementFile, replacement.Mode);
                        if (replaced)
                        {
                            WriteInfo("  ... succeeded!");
                        }
                        else
                        {
                            WriteWarning("  ... failed: file to be replaced wasn't found in list of files to be packed.");
                        }
                    }

                    WriteInfo("Replacement procedure completed.");
                }

                WriteInfo("Begin packing...");

                if (!project.OutPath.Exists)
                {
                    project.OutPath.Create();
                }

                string outputFilePath = Path.Combine(project.OutPath.FullName, project.PackedName + ".zip");

                using (IPacker packer = new ZipPacker())
                {
                    foreach (var pair in collector.GetAll())
                    {
                        if (pair.Item2.FullName == outputFilePath)
                        {
                            continue;
                        }

                        string archiveFileName = context.GetRelativePath(pair.Item1.FullName);

                        packer.Add(archiveFileName, pair.Item2.OpenRead());

                        WriteInfo("  Added '{0}'.", archiveFileName);
                    }

                    WriteInfo("Writing archive to '{0}'...", outputFilePath);

                    using (FileStream output = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
                    {
                        packer.Save(output);
                    }

                    WriteInfo("... success!");
                }
            }

            WriteInfo("");

            if (Properties.Settings.Default.InteractiveMode)
            {
                WriteInfo("");
                WriteInfo("Press any key to quit . . .");
                Console.ReadKey();
            }
        }
Example #2
0
    public static byte[] PackFiles(PackerFileFormat format, string[] srcFileNameList, string[] relativeNameList, ProgressDelegate?proc = null)
    {
        if (srcFileNameList.Length != relativeNameList.Length)
        {
            throw new ApplicationException("srcFileNameList.Length != relativeNameList.Length");
        }

        int num = srcFileNameList.Length;
        int i;

        ZipPacker zip = new ZipPacker();
        TarPacker tar = new TarPacker();

        for (i = 0; i < num; i++)
        {
            if (proc != null)
            {
                bool ret = proc(srcFileNameList[i], relativeNameList[i], i, num);

                if (ret == false)
                {
                    continue;
                }
            }

            byte[]   srcData = IO.ReadFile(srcFileNameList[i]);
            DateTime date    = File.GetLastWriteTime(srcFileNameList[i]);

            switch (format)
            {
            case PackerFileFormat.Tar:
            case PackerFileFormat.TarGZip:
                tar.AddFileSimple(relativeNameList[i], srcData, 0, srcData.Length, date);
                break;

            case PackerFileFormat.ZipRaw:
            case PackerFileFormat.ZipCompressed:
                zip.AddFileSimple(relativeNameList[i], date, FileAttributes.Normal, srcData, (format == PackerFileFormat.ZipCompressed));
                break;
            }
        }

        switch (format)
        {
        case PackerFileFormat.Tar:
            tar.Finish();
            return(tar.GeneratedData.Read());

        case PackerFileFormat.TarGZip:
            tar.Finish();
            return(tar.CompressToGZip());

        case PackerFileFormat.ZipCompressed:
        case PackerFileFormat.ZipRaw:
            zip.Finish();
            return(zip.GeneratedData.Read());

        default:
            throw new ApplicationException("format");
        }
    }