本文整理汇总了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();
}
}
}
示例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");
}
}
示例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;
}
示例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);
}
}
示例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);
}
}
}
示例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);
}
}
}
示例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;
}
示例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);
}
}
}
示例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));
}
}
}
示例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);
}
示例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");
}
}
示例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");
}
}
}
}
示例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;
}
示例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());
}
}
}
示例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");
}
}
}
}