我正在使用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
}
}
希望它能帮助你在连接到相应的事件时:
myCompressor.CompressionFinished += MyCompressorOnCompressionFinished;
myCompressor.FileCompressionStarted += MyCompressorOnFileCompressionStarted;
myCompressor.Compressing += MyCompressorOnCompressing;
他们可以帮助你获得更多的信息。
希望能有所帮助。