Exemple #1
0
        /// <summary>
        /// Writes a ZIP file to a stream, including the selected files from the static directory.
        /// </summary>
        /// <param name="directory">The static directory instance.</param>
        /// <param name="zipStream">The output stream.</param>
        /// <param name="searchPattern">
        /// Optionally specifies a file name pattern using standard file system wildcards
        /// like <b>[*]</b> and <b>[?]</b>.  This defaults to including all files.
        /// </param>
        /// <param name="searchOptions">Optionally perform a recursive search.  This defaults to
        /// <see cref="SearchOption.TopDirectoryOnly"/>.
        /// </param>
        /// <param name="zipOptions">
        /// Additional options that control things like whether the files are zipped within
        /// the parent directory or whether the files are assumed to contain UTF-8 text and
        /// that Windows style CRLF line endings are to be converted to Linux compatible LF
        /// endings.  You can combine options by bitwise ORing them.  This defaults to
        /// <see cref="StaticZipOptions.None"/>.
        /// </param>
        /// <remarks>
        /// <note>
        /// The current implementation loads the files into memory so this isn't really suitable
        /// for zipping very large files.
        /// </note>
        /// </remarks>
        public static void Zip(
            this IStaticDirectory directory,
            Stream zipStream,
            string searchPattern        = null,
            SearchOption searchOptions  = SearchOption.TopDirectoryOnly,
            StaticZipOptions zipOptions = StaticZipOptions.None)
        {
            Covenant.Requires <ArgumentNullException>(zipStream != null, nameof(zipStream));

            using (var zip = ZipFile.Create(zipStream))
            {
                zip.BeginUpdate();

                foreach (var file in directory.GetFiles(searchPattern, searchOptions))
                {
                    var relativePath = file.Path.Substring(directory.Path.Length + 1);

                    if ((zipOptions | StaticZipOptions.LinuxLineEndings) != 0)
                    {
                        var text = file.ReadAllText(Encoding.UTF8);

                        text = NeonHelper.ToLinuxLineEndings(text);

                        zip.Add(new StaticBytesDataSource(Encoding.UTF8.GetBytes(text)), relativePath);
                    }
                    else
                    {
                        zip.Add(new StaticBytesDataSource(file.ReadAllBytes()), relativePath);
                    }
                }

                zip.CommitUpdate();
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates a ZIP file, including the selected files from the static directory.
        /// </summary>
        /// <param name="directory">The static directory instance.</param>
        /// <param name="zipPath">Path to the output ZIP file.</param>
        /// <param name="searchPattern">
        /// Optionally specifies a file name pattern using standard file system wildcards
        /// like <b>[*]</b> and <b>[?]</b>.  This defaults to including all files.
        /// </param>
        /// <param name="searchOptions">Optionally perform a recursive search.  This defaults to
        /// <see cref="SearchOption.TopDirectoryOnly"/>.
        /// </param>
        /// <param name="zipOptions">
        /// Additional options that control things like whether the files are zipped within
        /// the parent directory or whether the files are assumed to contain UTF-8 text and
        /// that Windows style CRLF line endings are to be converted to Linux compatible LF
        /// endings.  You can combine options by bitwise ORing them.  This defaults to
        /// <see cref="StaticZipOptions.None"/>.
        /// </param>
        public static void Zip(
            this IStaticDirectory directory,
            string zipPath,
            string searchPattern        = null,
            SearchOption searchOptions  = SearchOption.TopDirectoryOnly,
            StaticZipOptions zipOptions = StaticZipOptions.None)
        {
            Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(zipPath), nameof(zipPath));

            using (var stream = new FileStream(zipPath, FileMode.CreateNew, FileAccess.ReadWrite))
            {
                Zip(directory, stream, searchPattern, searchOptions, zipOptions);
            }
        }