I am using C# and trying to use 7z to encrypt a single file into a new output archive.
I succeeded in encrypting a whole folder but not a file. Here is the code that does not work (i.e. after running the code output directory has no .7z file and no exceptions raised what so ever!)
My archiving class looks like this:
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
//}
}
}
and I call it from a button click like so:
private void button1_Click(object sender, EventArgs e)
{
Class1.compressFileTo7zip(@"d:\ddd.doc", @"d:\eee.7z");
}
the file d:\ddd.doc
does exist.
Just for the sake of completeness I will include my code that work for archiving directories:
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
}
}