本文整理汇总了C#中SevenZip.SevenZipCompressor.CompressFilesEncrypted方法的典型用法代码示例。如果您正苦于以下问题:C# SevenZipCompressor.CompressFilesEncrypted方法的具体用法?C# SevenZipCompressor.CompressFilesEncrypted怎么用?C# SevenZipCompressor.CompressFilesEncrypted使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SevenZip.SevenZipCompressor
的用法示例。
在下文中一共展示了SevenZipCompressor.CompressFilesEncrypted方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public override void Execute()
{
if (_cmdlet._directoryOrFilesFromPipeline == null) {
_cmdlet._directoryOrFilesFromPipeline = new List<string> {
_cmdlet.Path
};
}
var directoryOrFiles = _cmdlet._directoryOrFilesFromPipeline
.Select(path => new FileInfo(System.IO.Path.Combine(_cmdlet.SessionState.Path.CurrentFileSystemLocation.Path, path)).FullName).ToArray();
var archiveFileName = new FileInfo(System.IO.Path.Combine(_cmdlet.SessionState.Path.CurrentFileSystemLocation.Path, _cmdlet.ArchiveFileName)).FullName;
switch (System.IO.Path.GetExtension(archiveFileName).ToLowerInvariant()) {
case ".7z":
_cmdlet.Format = OutArchiveFormat.SevenZip;
break;
case ".zip":
_cmdlet.Format = OutArchiveFormat.Zip;
break;
case ".gz":
_cmdlet.Format = OutArchiveFormat.GZip;
break;
case ".bz2":
_cmdlet.Format = OutArchiveFormat.BZip2;
break;
case ".tar":
_cmdlet.Format = OutArchiveFormat.Tar;
break;
case ".xz":
_cmdlet.Format = OutArchiveFormat.XZ;
break;
}
var compressor = new SevenZipCompressor {
ArchiveFormat = _cmdlet.Format,
CompressionLevel = _cmdlet.CompressionLevel,
CompressionMethod = _cmdlet.CompressionMethod
};
_cmdlet.CustomInitialization?.Invoke(compressor);
var activity = directoryOrFiles.Length > 1
? $"Compressing {directoryOrFiles.Length} Files to {archiveFileName}"
: $"Compressing {directoryOrFiles.First()} to {archiveFileName}";
var currentStatus = "Compressing";
compressor.FilesFound += (sender, args) =>
Write($"{args.Value} files found for compression");
compressor.Compressing += (sender, args) =>
WriteProgress(new ProgressRecord(0, activity, currentStatus) { PercentComplete = args.PercentDone });
compressor.FileCompressionStarted += (sender, args) => {
currentStatus = $"Compressing {args.FileName}";
Write($"Compressing {args.FileName}");
};
if (directoryOrFiles.Any(path => new FileInfo(path).Exists)) {
var notFoundFiles = directoryOrFiles.Where(path => !new FileInfo(path).Exists).ToArray();
if (notFoundFiles.Any()) {
throw new FileNotFoundException("File(s) not found: " + string.Join(", ", notFoundFiles));
}
if (HasPassword) {
compressor.CompressFiles(archiveFileName, directoryOrFiles);
} else {
compressor.CompressFilesEncrypted(archiveFileName, _cmdlet.Password, directoryOrFiles);
}
}
if (directoryOrFiles.Any(path => new DirectoryInfo(path).Exists)) {
if (directoryOrFiles.Length > 1) {
throw new ArgumentException("Only one directory allowed as input");
}
if (_cmdlet.Filter != null) {
if (HasPassword) {
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, _cmdlet.Password, _cmdlet.Filter, true);
} else {
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, _cmdlet.Filter, true);
}
} else {
if (HasPassword) {
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName, _cmdlet.Password);
} else {
compressor.CompressDirectory(directoryOrFiles[0], archiveFileName);
}
}
}
WriteProgress(new ProgressRecord(0, activity, "Finished") { RecordType = ProgressRecordType.Completed });
Write("Compression finished");
}
示例2: Compress
private void Compress()
{
//setup settings.
try
{
_sevenZipCompressor = new SevenZipCompressor(tempPath);
_sevenZipCompressor.ArchiveFormat = _format;
_sevenZipCompressor.CompressionMethod = CompressionMethod.Default;
_sevenZipCompressor.DirectoryStructure = true;
_sevenZipCompressor.IncludeEmptyDirectories = true;
_sevenZipCompressor.FastCompression = _fastcompression;
_sevenZipCompressor.PreserveDirectoryRoot = false;
_sevenZipCompressor.CompressionLevel = _compresstionlevel;
_sevenZipCompressor.Compressing += Compressing;
_sevenZipCompressor.FileCompressionStarted += FileCompressionStarted;
_sevenZipCompressor.CompressionFinished += CompressionFinished;
_sevenZipCompressor.FileCompressionFinished += FileCompressionFinished;
try
{
if (_password != null)
{
for (int i = 0; i < _fileAndDirectoryFullPaths.Count; i++)
{
if (!Directory.Exists(_fileAndDirectoryFullPaths[i]))
continue;
//Compress directories
var strings = _fileAndDirectoryFullPaths[i].Split('/');
if (_fileAndDirectoryFullPaths.Count == 1)
{
_sevenZipCompressor.CompressDirectory(_fileAndDirectoryFullPaths[i],
String.Format("{0}/{1}.{2}",
_archivePath,
_archivename,
SelectExtention(_format)));
}
else
{
_sevenZipCompressor.CompressDirectory(_fileAndDirectoryFullPaths[i],
String.Format("{0}/{1}_{3}.{2}",
_archivePath,
_archivename,
SelectExtention(_format),
_fileAndDirectoryFullPaths[i].Split('\\')
.Last()),
_password);
}
//remove the directorys from the list so they will not be compressed as files as wel
_fileAndDirectoryFullPaths.Remove(_fileAndDirectoryFullPaths[i]);
}
//compress files
if (_fileAndDirectoryFullPaths.Count > 0)
{
_sevenZipCompressor.FileCompressionFinished += FileCompressionFinished;
_sevenZipCompressor.CompressFilesEncrypted(
String.Format("{0}/{1}.{2}",
_archivePath,
_archivename,
SelectExtention(_format)),
_password,
_fileAndDirectoryFullPaths.ToArray());
}
}
else
{
for (int i = 0; i < _fileAndDirectoryFullPaths.Count; i++)
//var fullPath in _fileAndDirectoryFullPaths)
{
FileInfo fi = new FileInfo(_fileAndDirectoryFullPaths[i]);
bytesoffiles += fi.Length;
if (!Directory.Exists(_fileAndDirectoryFullPaths[i]))
continue;
//Compress directorys
var strings = _fileAndDirectoryFullPaths[i].Split('/');
if (_fileAndDirectoryFullPaths.Count == 1)
{
_sevenZipCompressor.CompressDirectory(_fileAndDirectoryFullPaths[i],
String.Format("{0}/{1}.{2}",
_archivePath,
_archivename,
SelectExtention(_format)));
}
else
{
_sevenZipCompressor.CompressDirectory(_fileAndDirectoryFullPaths[i],
String.Format("{0}/{1}_{3}.{2}",
_archivePath,
_archivename,
SelectExtention(_format),
_fileAndDirectoryFullPaths[i].Split('\\')
.Last()));
}
//reset compression bar
//FileCompressionFinished(null, null);
//.........这里部分代码省略.........
示例3: compressFile
//.........这里部分代码省略.........
if (!string.IsNullOrEmpty(_encryptionPassword))
{
compressor.ZipEncryptionMethod = ZipEncryptionMethod.Aes256;
}
long lFreeSpace = 0;
lFreeSpace = Common.DriveFreeSpaceBytes(DestinationFolder);
//Check for Enough Free Space to compress the file
if (((file1.Length * 2) > lFreeSpace) && (lFreeSpace != -1))
{
_evt.WriteEntry("Compress: Not enough available free space to compress this file: " + file1.FullName, System.Diagnostics.EventLogEntryType.Error, 5140, 50);
compressor = null;
continue;
}
if (lFreeSpace == -1)
{
_evt.WriteEntry("Compress: Only files local to this machine should be compressed. Performance problem can occur with large files over the network. " + file1.FullName, System.Diagnostics.EventLogEntryType.Warning, 5150, 50);
}
//Compress or Compress and Encrypt Files
if (!string.IsNullOrEmpty(_encryptionPassword))
{
creader = new FileStream(str7File, FileMode.OpenOrCreate);
//Encrypt the file if password is specified
AES256 aes = new AES256(ep);
string upassword = aes.Decrypt(_encryptionPassword);
compressor.CompressFilesEncrypted(creader, upassword, strfilearr);
creader.Close();
creader.Dispose();
creader = null;
exreader = new FileStream(str7File, FileMode.Open);
extractor = new SevenZipExtractor(exreader, upassword);
upassword = "";
}
else
{
if (Common.IsFileLocked(file1))
{
_evt.WriteEntry("Compress: File is locked: " + file1.FullName, System.Diagnostics.EventLogEntryType.Error, 5070, 50);
continue;
}
creader = new FileStream(str7File, FileMode.OpenOrCreate);
compressor.CompressFiles(creader, strfilearr);
creader.Close();
creader.Dispose();
creader = null;
exreader = new FileStream(str7File, FileMode.Open);
extractor = new SevenZipExtractor(exreader);
}
//7Zip file ok?
blArchiveOk = extractor.Check();
exreader.Close();
exreader.Dispose();
exreader = null;
verifyArchive(blArchiveOk, str7File, file1.FullName);