当前位置: 首页>>代码示例>>C#>>正文


C# ZipArchive.CreateEntryFromFile方法代码示例

本文整理汇总了C#中System.IO.Compression.ZipArchive.CreateEntryFromFile方法的典型用法代码示例。如果您正苦于以下问题:C# ZipArchive.CreateEntryFromFile方法的具体用法?C# ZipArchive.CreateEntryFromFile怎么用?C# ZipArchive.CreateEntryFromFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Compression.ZipArchive的用法示例。


在下文中一共展示了ZipArchive.CreateEntryFromFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: using

 public static void Create
                         (
                             string zippedFileName
                             , IEnumerable<string> originalFiles
                             , Func<string, string> onCreateEntryProcessFunc//= null
                         )
 {
     using
             (
                 var fileStream = new FileStream
                                         (
                                             zippedFileName
                                             , FileMode.OpenOrCreate
                                             , FileAccess.Write
                                             , FileShare.None
                                         )
             )
     {
         using (ZipArchive archive = new ZipArchive(fileStream, ZipArchiveMode.Create))
         {
             foreach (var file in originalFiles)
             {
                 var entryName = onCreateEntryProcessFunc(file);
                 archive.CreateEntryFromFile(file, entryName);
             }
             //archive.Dispose();
         }
     }
 }
开发者ID:Microshaoft,项目名称:Microshaoft.Common.Utilities.Net.4x,代码行数:29,代码来源:ZipHelper.cs

示例2: CompressFile

 private void CompressFile()
 {
     if (File.Exists([email protected]"logs\battlelog\{date:yyyy-MM-dd}.log"))
     {
         using (var zipfile = new ZipArchive(File.Create([email protected]"logs\battlelog\{date:yyyy-MM-dd}.zip"), ZipArchiveMode.Create))
             zipfile.CreateEntryFromFile([email protected]"logs\battlelog\{date:yyyy-MM-dd}.log", $"{date:yyyy-MM-dd}.log");
         File.Delete([email protected]"logs\battlelog\{date:yyyy-MM-dd}.log");
     }
 }
开发者ID:huoyaoyuan,项目名称:AdmiralRoom,代码行数:9,代码来源:BattleDetailLogger.cs

示例3: Execute

        public override bool Execute()
        {
            if (File.Exists(DestinationArchive) && OverwriteDestination == true)
            {
                Log.LogMessage(MessageImportance.Low, "{0} already existed, deleting before zipping...", SourceDirectory, DestinationArchive);
                File.Delete(DestinationArchive);
            }

            Log.LogMessage(MessageImportance.High, "Compressing {0} into {1}...", SourceDirectory, DestinationArchive);
            if (!Directory.Exists(Path.GetDirectoryName(DestinationArchive)))
                Directory.CreateDirectory(Path.GetDirectoryName(DestinationArchive));

            if (ExcludePatterns == null)
            {
                ZipFile.CreateFromDirectory(SourceDirectory, DestinationArchive);
            }
            else
            {
                // convert to regular expressions
                Regex[] regexes = new Regex[ExcludePatterns.Length];
                for (int i = 0; i < ExcludePatterns.Length; ++i)
                    regexes[i] = new Regex(ExcludePatterns[i].ItemSpec, RegexOptions.IgnoreCase);

                using (FileStream writer = new FileStream(DestinationArchive, FileMode.CreateNew))
                {
                    using (ZipArchive zipFile = new ZipArchive(writer, ZipArchiveMode.Create))
                    {
                        var files = Directory.GetFiles(SourceDirectory, "*", SearchOption.AllDirectories);

                        foreach (var file in files)
                        {
                            // look for a match
                            bool foundMatch = false;
                            foreach (var regex in regexes)
                            {
                                if (regex.IsMatch(file))
                                {
                                    foundMatch = true;
                                    break;
                                }
                            }

                            if (foundMatch)
                            {
                                Log.LogMessage(MessageImportance.Low, "Excluding {0} from archive.", file);
                                continue;
                            }

                            var relativePath = MakeRelativePath(SourceDirectory, file);
                            zipFile.CreateEntryFromFile(file, relativePath, CompressionLevel.Optimal);
                        }
                    }
                }
            }

            return true;
        }
开发者ID:TerabyteX,项目名称:buildtools,代码行数:57,代码来源:ZipFileCreateFromDirectory.cs

示例4: CreateZipArchive

 private static void CreateZipArchive(string zipFileName, IEnumerable<string> fileNames)
 {
     using (var fileStream = File.Create(zipFileName))
     using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create))
     {
         foreach (var fileName in fileNames)
             zipArchive.CreateEntryFromFile(fileName, Path.GetFileName(fileName), CompressionLevel.Optimal);
     }
 }
开发者ID:NN---,项目名称:nuproj,代码行数:9,代码来源:Zip.cs

示例5: CreateArchive

 public static void CreateArchive(string inputFileName, string outputFileName)
 {
     using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.Write))
     {
         using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Create))
         {
             string entryName = Path.GetFileName(inputFileName);
             archive.CreateEntryFromFile(inputFileName, entryName, CompressionLevel.Optimal);
         }
     }
 }
开发者ID:MNLGD,项目名称:IDES-Data-Preparation-Dot-Net,代码行数:11,代码来源:ZipManager.cs

示例6: Test_ZipArchive_AddFile_01

 public static void Test_ZipArchive_AddFile_01(string zipFile, string file, string entryName, CompressionLevel compressionLevel = CompressionLevel.Optimal,
     FileMode fileMode = FileMode.OpenOrCreate)
 {
     using (FileStream fileStream = new FileStream(zipFile, fileMode))
     {
         using (System.IO.Compression.ZipArchive archive = new System.IO.Compression.ZipArchive(fileStream, ZipArchiveMode.Update))
         {
             archive.CreateEntryFromFile(file, entryName, compressionLevel);
         }
     }
 }
开发者ID:labeuze,项目名称:source,代码行数:11,代码来源:Test_ZipArchive.cs

示例7: CompressFile

 public string CompressFile(string path, string relativeTodir) {
     string zipFilePath = Path.GetTempFileName();
     using (FileStream zipStream = new FileStream(zipFilePath, FileMode.Create))
     using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) {
         string entryName = Path.GetFileName(path);
         if (!string.IsNullOrWhiteSpace(relativeTodir)) {
             entryName = path.MakeRelativePath(relativeTodir).Replace('\\', '/');
         }
         archive.CreateEntryFromFile(path, entryName);
     }
     return zipFilePath;
 }
开发者ID:Microsoft,项目名称:RTVS,代码行数:12,代码来源:FileSystem.cs

示例8: CreateZipArchive

 private static void CreateZipArchive(string workingDirectory, string zipFileName, IEnumerable<string> fileNames)
 {
     using (var fileStream = File.Create(zipFileName))
     using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create))
     {
         foreach (var fileName in fileNames)
         {
             var relativePath = GetRelativePath(fileName, workingDirectory);
             zipArchive.CreateEntryFromFile(fileName, relativePath, CompressionLevel.Optimal);
         }
     }
 }
开发者ID:sergey-steinvil,项目名称:xsddoc,代码行数:12,代码来源:Zip.cs

示例9: Create

        public void Create(ArgumentsReader argumentsReader)
        {
            var directoryName = Path.GetDirectoryName(argumentsReader.ImageFilePaths[1]);
            var zipFilePath = Path.Combine(directoryName, argumentsReader.DocumentFileNameWithoutExtension + ".sopimage");

            // ファイルの上書き確認
            if (File.Exists(zipFilePath))
            {
                var message = string.Format("既に「{0}」ファイルが存在します。上書きしてよろしいですか?", Path.GetFileName(zipFilePath));
                var result = MessageBox.Show(message, "ファイルの上書き確認", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    File.Delete(zipFilePath);
                }
                else
                {
                    MessageBox.Show("sopimageファイルの作成を中断しました。");

                    // 画像ファイルは消す。
                    deleteImageFiles(argumentsReader);
                    return;
                }
            }

            using (var fileStream = new FileStream(zipFilePath, FileMode.CreateNew))
            {
                using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
                {
                    foreach (var imageFilePath in argumentsReader.ImageFilePaths)
                    {
                        var newImageFileName = imageFilePath.Key + Path.GetExtension(imageFilePath.Value);
                        archive.CreateEntryFromFile(imageFilePath.Value, newImageFileName);
                    }
                }
            }

            // zipファイルが作成できたら、ファイルを削除
            deleteImageFiles(argumentsReader);

            // 完了ダイアログを表示。
            {
                var message = string.Format("「{0}」を作成しました。ファイルがあるフォルダを開きますか?", Path.GetFileName(zipFilePath));
                var result = MessageBox.Show(message, "sopimageファイルを作成しました。", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    System.Diagnostics.Process.Start(
                        "EXPLORER.EXE", string.Format(@"/select,""{0}""", zipFilePath));
                }
            }
        }
开发者ID:Yusuke-KOMIYAMA,项目名称:sop,代码行数:50,代码来源:ZipCreator.cs

示例10: GetCompressedLogsInternal

        protected override Task<Stream> GetCompressedLogsInternal()
        {
            var ms = new MemoryStream();

            using (var a = new ZipArchive(ms, ZipArchiveMode.Create, true))
            {
                foreach (var file in logFolder.GetFiles())
                {
                    a.CreateEntryFromFile(file.FullName, file.Name);
                }
            }

            ms.Position = 0;

            return Task.FromResult<Stream>(ms);
        }
开发者ID:onovotny,项目名称:MetroLog,代码行数:16,代码来源:FileTarget.cs

示例11: Test_OpenXml_Zip_01

 public static void Test_OpenXml_Zip_01(string docxFile, string directory, bool useSlash, bool addDirectoryEntry)
 {
     // ok    useSlash = false, addDirectoryEntry = false
     // bad   useSlash = false, addDirectoryEntry = true               le fichier est corrompu
     // ok    useSlash = true,  addDirectoryEntry = true
     // ok    useSlash = true,  addDirectoryEntry = false
     if (zFile.Exists(docxFile))
         zFile.Delete(docxFile);
     int l = directory.Length;
     if (!directory.EndsWith("\\"))
         l++;
     using (FileStream fs = new FileStream(docxFile, FileMode.OpenOrCreate))
     using (ZipArchive zipArchive = new ZipArchive(fs, ZipArchiveMode.Update, false, Encoding.UTF8))
     {
         int fileCount = 0;
         int directoryCount = 0;
         foreach (FileSystemInfo file in new DirectoryInfo(directory).EnumerateFileSystemInfos("*.*", SearchOption.AllDirectories))
         {
             string entryName = file.FullName.Substring(l);
             if (useSlash)
                 entryName = entryName.Replace('\\', '/');
             if ((file.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
             {
                 if (useSlash)
                     entryName = entryName + "/";
                 else
                     entryName = entryName + "\\";
                 if (addDirectoryEntry)
                 {
                     Trace.WriteLine($"add directory \"{entryName}\"");
                     ZipArchiveEntry entry = zipArchive.CreateEntry(entryName);
                     directoryCount++;
                 }
             }
             else
             {
                 Trace.WriteLine($"add file      \"{entryName}\"");
                 zipArchive.CreateEntryFromFile(file.FullName, entryName);
                 fileCount++;
             }
         }
         Trace.WriteLine($"total {fileCount + directoryCount} entries {fileCount} files {directoryCount} directories");
     }
 }
开发者ID:labeuze,项目名称:source,代码行数:44,代码来源:Test_OpenXml_Zip.cs

示例12: BackupFiles

        public static void BackupFiles(string filename)
        {
            var zipFile = new FileStream(filename, FileMode.Create);

            using (var archive = new ZipArchive(zipFile, ZipArchiveMode.Create))
            {
                foreach (var name in FileNames.Members)
                {
                    try
                    {
                        archive.CreateEntryFromFile(Path.Combine(SavePath, name), name);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("No file " + name + " generated to backup yet. Continuing...", "File Not Backed Up");
                    }
                }
            }
        }
开发者ID:kjrcda,项目名称:FinanceTracker,代码行数:19,代码来源:FileIO.cs

示例13: CompressFilesToZipArchive

        /// <summary>
        /// Compresses the files to zip archive.
        /// </summary>
        /// <param name="zipArchivePath">The zip file path.</param>
        /// <param name="filesToZip">The files to zip.</param>
        /// <returns></returns>
        /// Erstellt von Joshua Frey, am 15.01.2016
        public bool CompressFilesToZipArchive(string zipArchivePath, List<string> filesToZip)
        {
            using (FileStream zipToOpen = new FileStream(zipArchivePath, FileMode.OpenOrCreate))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
                {

                    foreach (string exportFilePath in filesToZip)
                    {
                        if (File.Exists(exportFilePath))
                        {
                            string fileName = Path.GetFileName(exportFilePath);
                            archive.CreateEntryFromFile(exportFilePath, fileName, CompressionLevel.Optimal);
                        }
                        else
                        {
                            throw new NWATException(MessageZipProblemFileMissing(exportFilePath));
                        }
                    }
                }
            }
            return true;
        }
开发者ID:KalumbaBatamba,项目名称:KalumbaNutz,代码行数:30,代码来源:ArchiveZipper.cs

示例14: BackupFolder

        private static void BackupFolder()
        {
            // create zip file using current date
            using (FileStream zipToOpen = File.Open(DateTime.Now.ToString() + Directory.GetCurrentDirectory()
                + ".zip", FileMode.Create))
            {
                // create archive
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
                {
                    // get list of files to be zipped, using current directory
                    var fileList = Directory.GetFiles((Directory.GetCurrentDirectory()));

                    // loop through each file in current directory and zip
                    foreach (var file in fileList)
                    {
                        archive.CreateEntryFromFile(file, file);
                    }

                // back up new zip file to folder
                    // FINISH - REPLACE NAME WITH NAME OF BACKUP DIRECTORY? NOW USING MSDN STRING NAME
                archive.ExtractToDirectory(Directory.GetCurrentDirectory());
                }
            }
        }
开发者ID:allisoncammack,项目名称:cs50x-cs,代码行数:24,代码来源:Program.cs

示例15: Compress_Logs

        private void Compress_Logs()
        {
            if (ServerSettings.RotateLogs_Zip)
            {
                //compress
                List<string> compress = new List<string>();
                foreach (string file in GetFileNames())
                {
                    if (!file.Contains(".zip"))
                    {
                        compress.Add(file);
                    }
                }

                //remove current logs file from compress list
                compress.Remove(new FileInfo(GetCurrentLogFile()).Name);

                foreach (string file_ext in compress)
                {
                    string file = file_ext.Replace("txt","");
                    string path = Path.Combine(GetDirectory(), file);

                    if (File.Exists(path + "txt"))
                    {
                        using (System.IO.FileStream fs = new System.IO.FileStream(@path + "zip", System.IO.FileMode.Create))
                        using (ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create))
                        {
                            arch.CreateEntryFromFile(@path + "txt", file_ext);
                        }

                        File.Delete(path + "txt");
                    }
                }

            }
        }
开发者ID:ElementalCrisis,项目名称:jmmserver,代码行数:36,代码来源:LogRotator.cs


注:本文中的System.IO.Compression.ZipArchive.CreateEntryFromFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。