Пример #1
0
        // Write back out to a msapp file.
        public static void SaveAsMsApp(CanvasDocument app, string fullpathToMsApp, ErrorContainer errors, bool isValidation = false)
        {
            app.ApplyBeforeMsAppWriteTransforms(errors);

            if (!fullpathToMsApp.EndsWith(".msapp", StringComparison.OrdinalIgnoreCase) &&
                fullpathToMsApp.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("Only works for .msapp files");
            }

            if (File.Exists(fullpathToMsApp)) // Overwrite!
            {
                File.Delete(fullpathToMsApp);
            }


            var checksum = new ChecksumMaker();

            DirectoryWriter.EnsureFileDirExists(fullpathToMsApp);
            using (var z = ZipFile.Open(fullpathToMsApp, ZipArchiveMode.Create))
            {
                foreach (FileEntry entry in app.GetMsAppFiles(errors))
                {
                    if (entry != null)
                    {
                        var e = z.CreateEntry(entry.Name.ToMsAppPath());
                        using (var dest = e.Open())
                        {
                            dest.Write(entry.RawBytes, 0, entry.RawBytes.Length);
                            checksum.AddFile(entry.Name.ToMsAppPath(), entry.RawBytes);
                        }
                    }
                }

                ComputeAndWriteChecksum(app, checksum, z, errors, isValidation);
            }

            // Undo BeforeWrite transforms so CanvasDocument representation is unchanged
            app.ApplyAfterMsAppLoadTransforms(errors);
        }