我正在使用C#并尝试使用7z将单个文件加密到新的输出存档中。
我成功加密了整个文件夹,但没有加密文件。这是不起作用的代码(即在运行代码输出目录之后没有.7z文件,并且没有异常引发了什么!)
我的归档课程如下所示:
class Class1
{
public static int compressFileTo7zip(string sourceFile, string destinationFile)
{ // takes the sourceFile and encrypt it with a password as destinationFile
//try
//{
//Console.WriteLine("compressFileTo7zip source File = " + sourceFile);
SevenZipCompressor myCompressor = new SevenZipCompressor();
myCompressor.DirectoryStructure = true;
myCompressor.ArchiveFormat = OutArchiveFormat.SevenZip;
SevenZipCompressor.SetLibraryPath(@"7z.dll");
myCompressor.CompressionMethod = CompressionMethod.Lzma;
myCompressor.EncryptHeaders = true;
myCompressor.IncludeEmptyDirectories = true;
myCompressor.VolumeSize = 15000000; // 15 mb segment
myCompressor.CompressionMode = CompressionMode.Create;
myCompressor.TempFolderPath = System.IO.Path.GetTempPath();
string myPassword = "2Hm3m3c2RKgkCjXyw7UGqhZh2EbezNM5EV"; // yes hardcoded ,just for debugging
// compress with password
myCompressor.CompressFilesEncrypted(destinationFile,myPassword, sourceFile );
//myCompressor.CompressFiles(destinationFile, sourceFile); // no output too !!
return 1;
//}
//catch (SevenZipLibraryException Ex)
//{
// Console.WriteLine("7zip 2nd merror message= " + Ex.Message);
// return -1; // an error occured ,return an indication of that
//}
}
}
我按下按钮就是这样说的:
private void button1_Click(object sender, EventArgs e)
{
Class1.compressFileTo7zip(@"d:\ddd.doc", @"d:\eee.7z");
}
文件d:\ddd.doc
确实存在。
为了完整起见,我将包含用于归档目录的代码:
public static int sourceDirectoryToFirstZipFile(string sourceDirectory, string destinationZip)
{
try
{
SevenZipCompressor myCompressor = new SevenZipCompressor();
myCompressor.DirectoryStructure = true;
myCompressor.ArchiveFormat = OutArchiveFormat.SevenZip;
myCompressor.CompressionMethod = CompressionMethod.Lzma;
myCompressor.EncryptHeaders = true;
myCompressor.IncludeEmptyDirectories = true;
SevenZipCompressor.SetLibraryPath(@"7z.dll");
myCompressor.CompressionMode = CompressionMode.Create;
myCompressor.TempFolderPath = System.IO.Path.GetTempPath();
string myPassword = "j4jkds98wlef04fw8nsfvi8svd9fwemjk"; //just debugging
// compress with password
myCompressor.CompressDirectory(sourceDirectory, destinationZip, myPassword);
return 1;
}
catch(SevenZipLibraryException Ex)
{
Console.WriteLine("7zip 1st merror message= " + Ex.Message);
return -1; // an error occured ,return an indication of that
}
}
答案 0 :(得分:1)
当你附加相应的事件时,它可以帮助你:
myCompressor.CompressionFinished += MyCompressorOnCompressionFinished;
myCompressor.FileCompressionStarted += MyCompressorOnFileCompressionStarted;
myCompressor.Compressing += MyCompressorOnCompressing;
他们可以帮助您获取更多信息。
希望它有所帮助。