I'am trying to unzip a file from netowrk in C# with this syntax:
string dezarhiverPath = ConfigurationSettings.AppSettings["PathWinZip"] + "\\WINZIP32.EXE";
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = dezarhiverPath;
pro.Arguments = " -e -j -o " + prmSource + " " + prmDestination;
Process x = Process.Start(pro);
x.WaitForExit();
If I use this sintax and my zip file is on local on my computer it works bbut when I move the path and i'am trying to unzip from a location on network doesn't work.Remain at line "x.WaitForExit()".If I try manualy to unzip it works.My user has rights to this location.I don't have error.
Can somebody help me to solve this problem?
Use external tool for unzipping like SharpZipLib because this way, you dont need to be depend of external tools like winzip.
Or even better : with .net 4.5 use System.IO.Compression.ZipFile, this is in assembly System.IO.Compression.FileSystem
For examples see MSDN site http://msdn.microsoft.com/en-us/library/ms404280(v=vs.110).aspx
Related
I need use 7zip in C#. Without console, just with 7zSharp.dll ?
+ I find some data here
http://7zsharp.codeplex.com/releases/view/10305 ,
but I don't know how to use it( - I could create .bat(.cmd) file, but I need throught dll file)
Exactly: I need extract .7z file with key)
Download the standalone console version from 7zip.com and add it to your project.
You need those 3 Files added in the project:
7za.exe
7za.dll
7zxa.dll
Don't forget to say Copy to Output Directory in it's preferences.
Extract an archive:
public void ExtractFile(string sourceArchive, string destination)
{
string zPath = "7za.exe"; //add to proj and set CopyToOuputDir
try
{
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = string.Format("x \"{0}\" -y -o\"{1}\"", sourceArchive, destination);
Process x = Process.Start(pro);
x.WaitForExit();
}
catch (System.Exception Ex) {
//handle error
}
}
Create an archive:
public void CreateZip(string sourceName, string targetArchive)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "7za.exe";
p.Arguments = string.Format("a -tgzip \"{0}\" \"{1}\" -mx=9", targetArchive, sourceName);
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
The authors of 7zip provide the LZMA SDK and good documentation which should be able to achieve what you want. The SDK includes C# code capable of compression / decompression.
Another option would be to use a something like C# (.NET) Interface for 7-Zip Archive DLLs
UPDATE:
Another user asked a similar question here: How do I create 7-Zip archives with .NET? The answer has several of the same links I provided and a few more.
Aspose.ZIP can be used to extract zip files with ease. Download it via NuGet package manager.
PM> Install-Package Aspose.Zip
The following code shows how to open or extract 7z file programmatically using C#:
using (SevenZipArchive archive = new SevenZipArchive("Sample.7z"))
{
archive.ExtractToDirectory(dataDir + "Sample_ExtractionFolder");
}
The code below explains how to extract or unzip a password protected 7zip file programmatically using C#:
using (SevenZipArchive archive = new SevenZipArchive("Sample_Encrypted.7z"))
{
archive.ExtractToDirectory("Sample_Encrypted7zip", "password");
}
Credit to Farhan Raza's blog post: https://blog.aspose.com/2021/04/28/open-extract-7zip-7z-file-unzip-in-csharp-asp-net/
It doesn't look like this library supports encrypted files. No method takes a key as a parameter.
The 7zSharp library doesn't seem to support password as input, just a zip file.
The library just calls the .exe of 7zip, so you could donwload the source and alter it to accept a password parameter which you then pass to the executable.
I want to download a file by just starting a new Process of Chrome. I've found the parameter "--download", so the solution should be "CHROMEPATH --download URI". ( http://peter.sh/experiments/chromium-command-line-switches/#download )
I wrote the code to start the process in C#, and yes, I know there are other options to do this like webclient, but I don't want to implement the download per se in my code.
string FILEURI = "example.org/file.png";
System.Diagnostics.Process prozess = new System.Diagnostics.Process();
prozess.StartInfo.FileName = #"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
prozess.StartInfo.Arguments = "--download " + FILEURI;
prozess.Start();
This works without any problem, but just opening the link "file://FILEURI". So I can't download it without any user interaction.
Chrome always downloads to the download folder. You could try to just get the latest download file, like this:
new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Downloads"))
.GetFiles()
.OrderByDescending(f => f.CreationTime)
.First()
This has risks (as outlined by DavidG) as well as the fact the user might download another file, etc. So this is not necessarily the best way.
I'm trying to create program to unzip files. I need to use winzip command line. I try to send in argument command to cmd, but it didn't work, because cmd didn't know my command. When I pasted manually command, it works.
var process = new ProcessStartInfo("cmd.exe");
var command = "/c WZUNZIP -spassword" + "\""+ "C:\my path\file.zip" + "\"" + " " + "\"" + "C:\my path" + "\"";
process.UseShellExecute = false;
process.Arguments = command;
Process.Start(process);
I tried to create .bat file and execute this file in my program, but like before it didn't work, when I executed it in my program and when start manually it works.
start cmd.exe /c WZUNZIP -spassword "C:\my path\file.zip" "C:\my path"
var process = new ProcessStartInfo("cmd.exe", pathToBatch);
Process.Start(process);
Mayby u know, the best way to execute .bat file in C#.
I need to use winzip, because only it provides encoding for my files. I tried to use DotNetZip and during uziping program threw exception that it can't be unziped, because library can't operate this files.
Apologies for adding as an answer (I currrently don't have enough rep for posting comments), but hopefully this will help.
What is the reason that you need to use winzip command line? Could you use ionic zip instead (http://dotnetzip.codeplex.com/ - also obtainable via NuGet in Visual Studio). I've used it a few times to zip files up and I know it unzips just as well.
The benefit I can see of not using the winzip command line is that you don't get the command prompt window popping up onscreen while the unzip is in progress.
Otherwise, as Noodles has suggested, the quotes must go around the whole path, not just the folders containing the spaces.
Edit: There is a similar SO post here: unzip file in C# via Winzip and its cmd extension
If you want to execute any thing you can use the process class with ProcessStartInfo.Arguments.
You can see more at:
MSDN: Process Class
MSDN: ProccessStartInfo.Arguments Property
And my question is because I don't understand for what you want it,
why don't you use system.IO.Compression or SharpzipLib? You can look up more info and download it with nugget.
Quotes go aroundthe entire path incl drive letter "c:\some folder\some file.zip"
To unzip
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
and to zip
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
and create a blank zip.
Set Ag=Wscript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Ag(0), 8, vbtrue)
BlankZip = "PK" & Chr(5) & Chr(6)
For x = 0 to 17
BlankZip = BlankZip & Chr(0)
Next
ts.Write BlankZip
Here's what works for me:
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", #"/c WZUNZIP.EXE -ye -o " + zipPath + " " + ExtractedFilesLocation);
procStartInfo.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
I need use 7zip in C#. Without console, just with 7zSharp.dll ?
+ I find some data here
http://7zsharp.codeplex.com/releases/view/10305 ,
but I don't know how to use it( - I could create .bat(.cmd) file, but I need throught dll file)
Exactly: I need extract .7z file with key)
Download the standalone console version from 7zip.com and add it to your project.
You need those 3 Files added in the project:
7za.exe
7za.dll
7zxa.dll
Don't forget to say Copy to Output Directory in it's preferences.
Extract an archive:
public void ExtractFile(string sourceArchive, string destination)
{
string zPath = "7za.exe"; //add to proj and set CopyToOuputDir
try
{
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = string.Format("x \"{0}\" -y -o\"{1}\"", sourceArchive, destination);
Process x = Process.Start(pro);
x.WaitForExit();
}
catch (System.Exception Ex) {
//handle error
}
}
Create an archive:
public void CreateZip(string sourceName, string targetArchive)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "7za.exe";
p.Arguments = string.Format("a -tgzip \"{0}\" \"{1}\" -mx=9", targetArchive, sourceName);
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
The authors of 7zip provide the LZMA SDK and good documentation which should be able to achieve what you want. The SDK includes C# code capable of compression / decompression.
Another option would be to use a something like C# (.NET) Interface for 7-Zip Archive DLLs
UPDATE:
Another user asked a similar question here: How do I create 7-Zip archives with .NET? The answer has several of the same links I provided and a few more.
Aspose.ZIP can be used to extract zip files with ease. Download it via NuGet package manager.
PM> Install-Package Aspose.Zip
The following code shows how to open or extract 7z file programmatically using C#:
using (SevenZipArchive archive = new SevenZipArchive("Sample.7z"))
{
archive.ExtractToDirectory(dataDir + "Sample_ExtractionFolder");
}
The code below explains how to extract or unzip a password protected 7zip file programmatically using C#:
using (SevenZipArchive archive = new SevenZipArchive("Sample_Encrypted.7z"))
{
archive.ExtractToDirectory("Sample_Encrypted7zip", "password");
}
Credit to Farhan Raza's blog post: https://blog.aspose.com/2021/04/28/open-extract-7zip-7z-file-unzip-in-csharp-asp-net/
It doesn't look like this library supports encrypted files. No method takes a key as a parameter.
The 7zSharp library doesn't seem to support password as input, just a zip file.
The library just calls the .exe of 7zip, so you could donwload the source and alter it to accept a password parameter which you then pass to the executable.
I want to open a file from a Class in C# using a Process, located in a directoy I asked the user.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "EXCEL.EXE";
startInfo.Arguments = Here goes the directory I asked
Process.Start(startInfo);
The problem, is that when the location of the file indicated by the user has an space," ", excel thinks that I'm sending two sepparate locations. For example with C:\Users\dj\Desktop\da ba excel tries to open " C:\Users\dj\Desktop\da" as one file, and at the same time "ba" as another file. How can I send a location to excel which has an space, without having this error? with an addres like C:\Users\dj\Desktop\daba without an space it works perfectly.
Try quoting your path:
startInfo.Arguments = "\"" + "C:\Users\dj\Desktop\da ba.xls" + "\"";
Tim
Try using a string literal
startInfo.Arguments = #"C:\Users\un\Desktop\file with space"
This way works
"\"" + #dialog.FileName + "\"";