sfx files do extract though input the wrong password - c#

I have sfx files from the program that I created using sevenzipsharp library. still when I execute directly with double-click the file sfx if using the wrong password but still extract the files in it with a size of 0 bytes, if anyone should I add another mode to function 'Compress' so that when I execute the file sfx wrong password files are not extracted at all.
Compress code:
public void Compress()
{
SevenZipCompressor.SetLibraryPath("7z.dll");
SevenZipCompressor cmp = new SevenZipCompressor();
cmp.Compressing += new EventHandler<ProgressEventArgs>(cmp_Compressing);
cmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>(cmp_StartCompress);
cmp.CompressionFinished += new EventHandler<EventArgs>(cmp_CompleteCompressed);
cmp.ArchiveFormat = OutArchiveFormat.SevenZip;
cmp.CompressionLevel = CompressionLevel.Normal;
cmp.CompressionMethod = CompressionMethod.Lzma;
cmp.CompressionMode = CompressionMode.Create;
string password = txtPasswordEn.Text;
string DirFile = tempFolder;
string NameFileCompress = Path.Combine(txtOutputFileEn.Text, txtNameFile.Text) + (".zip");
cmp.BeginCompressDirectory(DirFile, NameFileCompress, password, ".",true);
}
Create SFX Code:
public void CreateSfx()
{
string location = Path.Combine(txtOutputFileEn.Text, txtNameFile.Text);
string nameZip = location + (".zip");
string nameExe = location + (".exe");
SfxModule mdl = SfxModule.Extended;
SevenZipSfx sfx = new SevenZipSfx(mdl);
sfx.ModuleFileName = #"7z.sfx";
sfx.MakeSfx(nameZip, nameExe);
}

I've just seen that you're not creating a .zip file but a .7z file (and then convert it to a self extracting archive).
For that file format, you can achieve file name encryption using the EncryptHeaders property:
cmp.EncryptHeaders = true;

Related

How to Decrypt, incryptdumpfile in mysql using C# (MySqlBackup.dll)

I want to Decrypt the Mysql Incrypted dump file with mysqlbackup.dll. I use bc.DecryptDumpFile() but this show error "Incorrect password or corrupted context" How to decrypt this file
MySqlBackup bc = new MySqlBackup();
bc.DecryptDumpFile(Application.StartupPath + "//POS_Assistant - 24-Jul-19.dll", Application.StartupPath + "//POS.sql", "Rehman92");
Maybe the below code helps:
private void DecryptDumpFile()
{
string oldDumpFile = "C:\\backup.sql";
string newDumpFile = "C:\\backup_new.sql";
MySqlBackup mb = new MySqlBackup();
mb.EnableEncryption = true;
mb.EncryptionKey = "qwerty";
mb.DecryptSqlDumpFile(oldDumpFile, newDumpFile);
}

create textfile dynamically if textfile size exceeds maximum size

I'm writing text to files using StreamWriter using the following code:
path == #"Desktop\";
filepath1 = path + "1.txt";
StreamWriter _sw = new StreamWriter(filepath1, true);
_sw.WriteLine("some Text");
_sw.Close();
if size of textfile exceeds 500kb I want to create text files dynamically. I'm tryng following code:
var size = (path.Length)/1024;
if(size>=500)
{
int i = (size/500)+1;
var filepath2 = path + i + ".txt";
if (File.Exists(filepath2))
{
StreamWriter _sw = new StreamWriter(filepath2, true);
_sw.WriteLine("Some message");
_sw.Close();
}
}
else
{
FileStream fs = File.Create(filepath2);
StreamWriter _sw = new StreamWriter(filepath2, true);
_sw.WriteLine(ex);
_sw.Close();
}
My question is if file 2.txt also exceeds 500kb I want to create 3.txt,4.txt..... and so on..
I want to create all these dynamically - how to solve this problem?
First thing you need to do the SIZE comparison for the data length of File not the File Path.
Here is Function which dose what you want to achieve, Please make appropriate changes for your path.
//Public variable to manage file names
int FileCounter = 1;
string FileName;
// Call this function to Add text to file
private void WriteToFile(string writeText)
{
FileName = "MyFile_"+FileCounter +".txt";
if (File.Exists(FileName))
{
string str = File.ReadAllText(FileName);
if ((str.Length + writeText.Length) / 1024 > 500) // check for limit
{
// Create new File
FileCounter++;
FileName = "MyFile_" + FileCounter + ".txt";
StreamWriter _sw = new StreamWriter(FileName, true);
_sw.WriteLine(writeText);
_sw.Close();
}
else // use exixting file
{
StreamWriter _sw = new StreamWriter(FileName, true);
_sw.WriteLine(writeText);
_sw.Close();
}
}
}
Where to start..
You are writing it as one big long procedural script. You need to break it down into chunks that can be reused using functions. As it is, it will get out of control way too quickly.
path == #"Desktop\"; is not valid. 1 too many =
Use Path.Combine() to combine your folder and filenames.
I'm sure this is all just test/rough/scratch code but just in case it's not, also check out Try/Except to wrap your file handling. You should also look up using() to dispose of your streams/writers.
My last comment would be that I see a lot of this sort of code a lot and it's often replaceable with something like Nlog for a whole lot less friction.
I would have commented but this login has no rep.

C# - Copy File to new location, then read in PDF (With Code)

In the code below, the console prompts the user for 2 files (currently in a networked location). It then copies those files to the local drive for quicker reading of the PDF, but I'm running into a problem. If I reference the last line of code as PdfDocument pdf = new PdfDocument("C:\somepdf.pdf"); the file is accessed extremely quickly.
However, with the current copy processes, for some reason, this line of code alone is taking upwards of 18-20 minutes to process. I'm assuming that this is because the file, having recently been copied, is still locked under a process, even though the actual copy process takes less than 10 seconds.
In my research, I have seen various ways of identifying the process that's locking the file and killing it, but this doesn't seem to apply to what I'm trying to do.
Unfortunately, I'm to the point where I have to ask for help. Am I overlooking something here? I don't see why it would take 15 less minutes to process a pdf referenced locally, than one processed by a copy process, then locally.
Thoughts?
string selectFileNameO;
string selectFileNameF;
string FileNameO;
string FileNameF;
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Title = "Choose File";
dialog.FileName = "";
dialog.ShowDialog();
selectFileNameO = dialog.FileName;
}
string ext = System.IO.Path.GetExtension(selectFileNameO);
selectFileNameF = Path.GetFileName(selectFileNameO);
selectFileNameF = selectFileNameF.Substring(0, selectFileNameF.Length - ext.Length);
selectFileNameF = "C:\\" + selectFileNameF + ".ext";
Console.WriteLine(selectFileNameF);
using (OpenFileDialog dialog2 = new OpenFileDialog())
{
dialog2.Title = "Choose 2 File";
dialog2.FileName = "";
dialog2.ShowDialog();
FileNameO = dialog2.FileName;
}
string ext1 = System.IO.Path.GetExtension(FileNameO);
FileNameF = Path.GetFileName(FileNameO);
FileNameF = FileNameF.Substring(0, FileNameF.Length - ext1.Length);
FileNameF = "C:\\" + FileNameF + ".ext";
File.Copy(FileNameO, FileNameF, true);
int distanceToString = 535;
int lengthOfString = 6;
string myDataSet;
using (StreamReader sr = new StreamReader(selectFileNameF))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
myDataSet = line.Substring(distanceToString, lengthOfString);
selectFileUIDs.Add(myDataSet);
Console.WriteLine(myDataSet);
}
sr.Dispose();
}
Console.WriteLine(FileNameF);
PdfDocument pdf = new PdfDocument(FileNameF);

SevenZipSharp library: Cannot encrypt headers

My task is to create the password protected ZIP with the SevenZipSharp library.
I managed to make the files contents locked with the password, however the archive structure - file names, directories hierarchy can be viewed in any of the WinZip, 7-Zip or Compressed folder.
I use the cmp.EncryptHeaders = true; however it seems to have no effect...
How can I encrypt the files and directories names? Thanks.
static void Main(string[] args)
{
const string LibraryPath = #"C:\Program Files\7-Zip\7z.dll";
SevenZipCompressor.SetLibraryPath(LibraryPath);
var cmp = new SevenZipCompressor();
cmp.CompressionMethod = CompressionMethod.Default;
cmp.CompressionLevel = CompressionLevel.Fast;
cmp.ArchiveFormat = OutArchiveFormat.Zip; // compatible with WinZip and Compressed folder
cmp.ZipEncryptionMethod = ZipEncryptionMethod.ZipCrypto; // compatible with old WinZip
cmp.EncryptHeaders = true;
cmp.FileCompressionStarted += (sender, e) =>
{
Console.WriteLine(((FileNameEventArgs)e).FileName);
};
const string archive = #"C:\temp\12.3G.zip";
File.Delete(archive);
cmp.CompressDirectory(#"C:\temp\Photos", archive, "password");
}
Looking at the source code, it appears the only way for that flag to take effect is to use SevenZip for the OutArchiveFormat.
From the source code:
if (EncryptHeaders && _archiveFormat == OutArchiveFormat.SevenZip && !SwitchIsInCustomParameters("he"))
{
names.Add(Marshal.StringToBSTR("he"));
var tmp = new PropVariant {VarType = VarEnum.VT_BSTR, Value = Marshal.StringToBSTR("on")};
values.Add(tmp);
}

Mixing and converting wav files to one mp3 file

I have 2 wav files. It should convert them to one mp3 file using naudio and lame.exe. Note that the wav file should be created by mixing 2 wav files (not concatenating).
two wav files => one mp3 file
private void MixWavFiles(string[] inputFiles, string outFileName)
{
int count = inputFiles.GetLength(0);
WaveMixerStream32 mixer = new WaveMixerStream32();
WaveFileReader[] reader = new WaveFileReader[count];
WaveChannel32[] channelSteam = new WaveChannel32[count];
mixer.AutoStop = true;
for (int i = 0; i < count; i++)
{
reader[i] = new WaveFileReader(inputFiles[i]);
channelSteam[i] = new WaveChannel32(reader[i]);
mixer.AddInputStream(channelSteam[i]);
}
mixer.Position = 0;
WaveFileWriter.CreateWaveFile(outFileName, mixer);
}
private string ConvertWavToMp3(string wavFileName)
{
string mp3FileName = Path.ChangeExtension(wavFileName, "mp3").Replace(Directory.GetCurrentDirectory(), "c:");
string commandLine = " -V2 " + wavFileName + " " + mp3FileName;
var lamaProcessInfo = new ProcessStartInfo();
lamaProcessInfo.Arguments = commandLine;
lamaProcessInfo.FileName = WavToMp3ConverterFileName;
lamaProcessInfo.WindowStyle = ProcessWindowStyle.Minimized;
using (var lamaProcess = Process.Start(lamaProcessInfo))
{
lamaProcess.WaitForExit();
int exitCode = lamaProcess.ExitCode;
lamaProcess.Close();
}
return mp3FileName;
}
Well, my this is how I'm doing that:
First I'm mixing 2 wav files using NAudio and getting one mixed wav
file.
Then I'm converting this wav file to mp3 file using lame.exe.
At the second step exitCode always equals 1 and it means there is an error. So I'm unable to convert wav file (mixed) to mp3 (result) file.
But if I'm converting each of two wav files to two mp3 files it works fine! And exitCode equals 0. So I have a conclusion the commandLine for converting one (mixed) wav file to mp3 file is wrong. Or the mixed wav has the wrong format but it's not most likely because it can be played by winamp.
Does anyone have any suggestions?

Categories

Resources