私は 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
}
}