using 7zip in c#

T

tuco

There are 100% managed solutions for zip files, take a lookat
i have problem with ICsharpzip with big files, i use 7za.exe and problem
resolved.
 
N

nologo

All,
i'm using 7zip to archive data that my application produces, the
following code fails to work. The problem is, the code seems to
redirect where its looking for the 7z.exe to where-ever the
SaveFileDialog is pointed at to save the document.
if (this.saveFileDialog1.ShowDialog(this) != DialogResult.Cancel)
{
string strFileLocation = "C:\\temp\\";
string strFileName;
string strFileToZip = "c:\\temp\\reports";
string strSB;

strFileName = this.saveFileDialog1.FileName;
StringBuilder SB = new StringBuilder();
SB.Append ("/c ");
SB.Append ("7za ");
SB.Append ("a ");
SB.Append (strFileLocation);
SB.Append (strFileName);
SB.Append (" ");
SB.Append (strFileToZip);
strSB = SB.ToString();


ProcessStartInfo psiOpt = new ProcessStartInfo(@"cmd.exe", strSB);
psiOpt.WindowStyle = ProcessWindowStyle.Normal;
psiOpt.RedirectStandardOutput = true;
psiOpt.UseShellExecute = false;
psiOpt.CreateNoWindow = true;
// Create the actual process object
Process procCommand = Process.Start(psiOpt);
// Receives the output of the Command Prompt
StreamReader srIncoming = procCommand.StandardOutput;
// Show the result
MessageBox.Show(srIncoming.ReadToEnd());
// Close the process
procCommand.WaitForExit();
}

if i dont use the savefiledialog and use:
strFileName = C:\temp\archive%DATE:/=_%.zip, this works perfect to the
preset location in the string text.
Any ideas how i can retain the .exe location wihtout it changing by
using savefiledialog?
Cheers
 
M

Marc Gravell

Have you tried setting psiOpt.WorkingDirectory ?

Of course, I'd be tempted to use zip for compatibility; #ziplib for
example can be run directly as managed code, removing any pre-
requisites and IPC issues.

Marc
 
I

Ignacio Machin ( .NET/ C# MVP )

All,
i'm using 7zip to archive data that my application produces, the
following code fails to work.  The problem is, the code seems to
redirect where its looking for the 7z.exe to where-ever the
SaveFileDialog is pointed at to save the document.
if (this.saveFileDialog1.ShowDialog(this) != DialogResult.Cancel)
{
   string strFileLocation = "C:\\temp\\";
   string strFileName;
   string strFileToZip = "c:\\temp\\reports";
   string strSB;

   strFileName = this.saveFileDialog1.FileName;
   StringBuilder SB = new StringBuilder();
   SB.Append ("/c ");
   SB.Append ("7za ");
   SB.Append ("a ");
   SB.Append (strFileLocation);
   SB.Append (strFileName);
   SB.Append (" ");
   SB.Append (strFileToZip);
   strSB = SB.ToString();

   ProcessStartInfo psiOpt = new ProcessStartInfo(@"cmd.exe", strSB);
   psiOpt.WindowStyle = ProcessWindowStyle.Normal;
   psiOpt.RedirectStandardOutput = true;
   psiOpt.UseShellExecute = false;
   psiOpt.CreateNoWindow = true;
   // Create the actual process object
   Process procCommand = Process.Start(psiOpt);
   // Receives the output of the Command Prompt
   StreamReader srIncoming = procCommand.StandardOutput;
   // Show the result
   MessageBox.Show(srIncoming.ReadToEnd());
   // Close the process
   procCommand.WaitForExit();

}

if i dont use the savefiledialog and use:
strFileName = C:\temp\archive%DATE:/=_%.zip, this works perfect to the
preset location in the string text.
Any ideas how i can retain the .exe location wihtout it changing by
using savefiledialog?
Cheers

Hi,

There are 100% managed solutions for zip files, take a lookat
ICsharpzip library
 
N

nologo

Have you tried setting psiOpt.WorkingDirectory ?

Of course, I'd be tempted to use zip for compatibility; #ziplib for
example can be run directly as managed code, removing any pre-
requisites and IPC issues.

Marc

hmm #ziplib, i'll look into it. have been trying to find a freeware
zip application.
 
B

Ben Voigt [C++ MVP]

nologo said:
cheers all,
with regards to my initial question:
psiOpt.WorkingDirectory = [directory]
resolved the issue, but its not ideal.
i'm looking into
#ziplibhttp://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

psiOpt.WorkingDirectory = [directory]
how do i make a reference to the starting location of the application?

For example, read Environment.CurrentDirectory when the application starts
and save that in a string variable.
Or Assembly.GetEntryAssembly().CodeBase if you want the directory containing
your application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top