Exemple #1
0
        public static void Run(string targetPath, string packName, PackagingSetting setting, IEnumerable <string> ignorePath, string key = "")
        {
            var version = PackageVersion;

            Uri directoryUri = new Uri(Path.GetFullPath(targetPath).RemoveLastDirectorySeparator());

            if (Path.GetExtension(packName) != PackFile.Extension)
            {
                packName += PackFile.Extension;
            }


            ErrorCheck(directoryUri);

            var internalFormat = new List <InternalHeader>();

            uint offset     = 0;
            uint headerSize = 0;

            foreach (Uri uri in EnumerateAllFiles(directoryUri, setting))
            {
                uint size = 0;
                using (var stream = File.Open(uri.LocalPath, FileMode.Open))
                {
                    size = (uint)stream.Length;
                }

                InternalHeader tmp = new InternalHeader(size,
                                                        offset,
                                                        // *.pack/hoge/fuga の *.pack/ を削除
                                                        new string(directoryUri.MakeRelativeUri(uri).ToString()
                                                                   .SkipWhile(ch => ch != Path.AltDirectorySeparatorChar && ch != Path.DirectorySeparatorChar)
                                                                   .Skip(1)
                                                                   .ToArray()));
                internalFormat.Add(tmp);
                headerSize += tmp.HeaderSize;
                offset     += size;
            }
            TopHeader format = new TopHeader((uint)internalFormat.Count, ignorePath
                                             .Where(path => !String.IsNullOrEmpty(path))
                                             .Select(path => directoryUri.MakeRelativeUri(new Uri(Path.GetFullPath(path))).ToString()), version);

            internalFormat.ForEach(inHeader => inHeader.Offset += headerSize + format.HeaderSize);

            PrintHeaderInfo(format, internalFormat, packName);

            string exportedPath = string.Empty;

            if (System.IO.Path.IsPathRooted(packName))
            {
                exportedPath = packName;
            }
            else
            {
                exportedPath = Path.GetDirectoryName(directoryUri.LocalPath)
                               // 実行ファイルと同じ階層に生成したい場合は GetFileName に変更
                               + Path.DirectorySeparatorChar
                               + packName;
            }

            using (var writer = new BinaryWriter(File.Create(exportedPath)))
            {
                writer.Write(format.ToByteArray());
                internalFormat.ForEach(inHeader => writer.Write(inHeader.ToByteArray()));

                foreach (string fullPath in EnumerateAllFiles(directoryUri, setting)
                         .Select(path => new Uri(directoryUri, path).LocalPath))
                {
                    writer.Write(File.ReadAllBytes(fullPath));
                }
            }

            if (!string.IsNullOrEmpty(key))
            {
                Encrypt(exportedPath, key, version);
            }
        }
Exemple #2
0
        public static void Run(string targetPath, string packName, PackagingSetting setting, IEnumerable<string> ignorePath, string key = "")
        {
            var version = PackageVersion;

            Uri directoryUri = new Uri(Path.GetFullPath(targetPath).RemoveLastDirectorySeparator());

            if (Path.GetExtension(packName) != PackFile.Extension)
            {
                packName += PackFile.Extension;
            }

            ErrorCheck(directoryUri);

            var internalFormat = new List<InternalHeader>();

            uint offset = 0;
            uint headerSize = 0;

            foreach (Uri uri in EnumerateAllFiles(directoryUri, setting))
            {
                uint size = 0;
                using (var stream = File.Open(uri.LocalPath, FileMode.Open))
                {
                    size = (uint)stream.Length;
                }

                InternalHeader tmp = new InternalHeader(size,
                                                        offset,
                                                        // *.pack/hoge/fuga の *.pack/ を削除
                                                        new string(directoryUri.MakeRelativeUri(uri).ToString()
                                                                    .SkipWhile(ch => ch != Path.AltDirectorySeparatorChar && ch != Path.DirectorySeparatorChar)
                                                                    .Skip(1)
                                                                    .ToArray()));
                internalFormat.Add(tmp);
                headerSize += tmp.HeaderSize;
                offset += size;
            }
            TopHeader format = new TopHeader((uint)internalFormat.Count, ignorePath
                .Where(path => !String.IsNullOrEmpty(path))
                .Select(path => directoryUri.MakeRelativeUri(new Uri(Path.GetFullPath(path))).ToString()), version);

            internalFormat.ForEach(inHeader => inHeader.Offset += headerSize + format.HeaderSize);

            PrintHeaderInfo(format, internalFormat, packName);

            string exportedPath = string.Empty;
            if (System.IO.Path.IsPathRooted(packName))
            {
                exportedPath = packName;
            }
            else
            {
                exportedPath = Path.GetDirectoryName(directoryUri.LocalPath)
                    // 実行ファイルと同じ階層に生成したい場合は GetFileName に変更
                    + Path.DirectorySeparatorChar
                    + packName;
            }

            using (var writer = new BinaryWriter(File.Create(exportedPath)))
            {
                writer.Write(format.ToByteArray());
                internalFormat.ForEach(inHeader => writer.Write(inHeader.ToByteArray()));

                foreach (string fullPath in EnumerateAllFiles(directoryUri, setting)
                    .Select(path => new Uri(directoryUri, path).LocalPath))
                {
                    writer.Write(File.ReadAllBytes(fullPath));
                }
            }

            if (!string.IsNullOrEmpty(key))
            {
                Encrypt(exportedPath, key, version);
            }
        }