CreateFromDirectory() public static method

Creates a Zip archive at the path destinationArchiveFileName that contains the files and directories from the directory specified by sourceDirectoryName. The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive will be created. If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method does not include the base directory into the archive. If an error is encountered while adding files to the archive, this method will stop adding files and leave the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid Zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.

If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name.

Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default. (Currently, the underlying compression algorithm is provided by the System.IO.Compression.DeflateStream class.)

sourceDirectoryName or destinationArchiveFileName is a zero-length /// string, contains only white space, or contains one or more invalid characters as defined by /// InvalidPathChars. sourceDirectoryName or destinationArchiveFileName is null. In sourceDirectoryName or destinationArchiveFileName, the specified /// path, file name, or both exceed the system-defined maximum length. /// For example, on Windows-based platforms, paths must be less than 248 characters, and file /// names must be less than 260 characters. The path specified in sourceDirectoryName or destinationArchiveFileName /// is invalid, (for example, it is on an unmapped drive). /// -OR- The directory specified by sourceDirectoryName does not exist. destinationArchiveFileName already exists. /// -OR- An I/O error occurred while opening a file to be archived. destinationArchiveFileName specified a directory. /// -OR- The caller does not have the required permission. sourceDirectoryName or destinationArchiveFileName is /// in an invalid format.
public static CreateFromDirectory ( string sourceDirectoryName, string destinationArchiveFileName ) : void
sourceDirectoryName string The path to the directory on the file system to be archived.
destinationArchiveFileName string The name of the archive to be created.
return void
        private void generateWindowsPortable(Console console)
        {
            console.WriteLine("==================================");
            console.WriteLine("Generating Windows Portable Deploy...");
            var zipPath = textDeployFolder.Text + "\\" + textNamespace.Text + "." + textVersion.Text;

            if (chkDemo.Checked)
            {
                zipPath += "." + textDemoTag.Text;
            }
            zipPath += ".Windows.Portable.zip";
            var tempPath = textDeployFolder.Text + "\\" + textTitle.Text;

            var files = Directory.GetFiles(textReleaseFolder.Text, "*.*", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                var fileRelative = file;
                fileRelative = fileRelative.Replace(textReleaseFolder.Text, "");
                var fileDir = new FileInfo(tempPath + fileRelative);
                fileDir.Directory.Create();

                File.Copy(file, tempPath + fileRelative, true);

                console.WriteLine("Copy To Temp: " + file);
            }
            console.WriteLine("Creating Zip File..." + zipPath);
            if (File.Exists(zipPath))
            {
                File.Delete(zipPath);
            }
            ZipFile.CreateFromDirectory(tempPath, zipPath, CompressionLevel.Optimal, true);
            console.Write("Deleting Temp Folder...");
            Directory.Delete(tempPath, true);
            console.WriteLine("OK");
        }
Ejemplo n.º 2
0
        public string DownloadZip(string filesToDownload, string archName, string productId, string pathToFilesFolder)
        {
            List <string> files            = filesToDownload.Split(';').ToList();
            var           curDate          = DateTime.Now.ToShortDateString().Replace("-", "").Replace(":", "").Replace(".", "").Replace("\\", "").Replace("/", "");
            var           archiveOutFolder = AppDomain.CurrentDomain.BaseDirectory + ($"\\tempout\\{curDate}\\{productId}\\");
            var           tempFolder       = AppDomain.CurrentDomain.BaseDirectory + "\\temp\\";

            if (!Directory.Exists(archiveOutFolder))
            {
                Directory.CreateDirectory(archiveOutFolder);
            }
            var archive = string.Concat(archiveOutFolder, archName);

            ClearTempDirectories(curDate, tempFolder);
            files.ForEach(f => CopyFile(pathToFilesFolder, f, tempFolder));

            if (File.Exists(archive))
            {
                File.Delete(archive);
            }
            ZipFileCustom.CreateFromDirectory(tempFolder, archive, CompressionLevel.Optimal, false);

            return($"../tempout/{curDate}/{productId}/{archName}");
        }