Cannot read that as a ZipFile for .exe file C# - c#

I'm using SevenZIP library files to unzip/extract .exe file. When i tried this approcah I'm getting a error Cannot read that as a ZipFile & zip exception was unhanded. I don't want to use any 7zip.exe console app in my project & i prefer to use .dll files in my project.
Is there any other way to extract .exe file?
private void MyExtract()
{
if(x86)
ExtractZip(#"D:\22.1.2.702\64\953-win_x86.exe", ".");
else
ExtractZip(#"D:\22.1.2.702\64\.702-win_x64.exe", ".");
}
private void ExtractZip(string zipFile, string directory)
{
using (var zip1 = ZipFile.Read(zipFile))
{
// here, we extract every entry, but we could extract conditionally
// based on entry name, size, date, checkbox status, etc.
foreach (var e in zip1)
{
e.Extract(directory, ExtractExistingFileAction.OverwriteSilently);
}
}
}

Code sample looks like you are using DotNetZip and not SevenZipLib. DotNetZip can only extract .zip files, not 7-zip nor .exe.

Instead of using SevenZip lib, try 7zip.exe in console. Use Process class to execute 7zip.exe. It works perfect.

Related

Download and Decompress zip file error: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream [duplicate]

I am trying to programatically unzip a zipped file.
I have tried using the System.IO.Compression.GZipStream class in .NET, but when my app runs (actually a unit test) I get this exception:
System.IO.InvalidDataException: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream..
I now realize that a .zip file is not the same as a .gz file, and that GZip is not the same as Zip.
However, since I'm able to extract the file by manually double clicking the zipped file and then clicking the "Extract all files"-button, I think there should be a way of doing that in code as well.
Therefore I've tried to use Process.Start() with the path to the zipped file as input. This causes my app to open a Window showing the contents in the zipped file. That's all fine, but the app will be installed on a server with none around to click the "Extract all files"-button.
So, how do I get my app to extract the files in the zipped files?
Or is there another way to do it? I prefer doing it in code, without downloading any third party libraries or apps; the security department ain't too fancy about that...
With .NET 4.5 you can now unzip files using the .NET framework:
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string startPath = #"c:\example\start";
string zipPath = #"c:\example\result.zip";
string extractPath = #"c:\example\extract";
System.IO.Compression.ZipFile.CreateFromDirectory(startPath, zipPath);
System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
}
The above code was taken directly from Microsoft's documentation: http://msdn.microsoft.com/en-us/library/ms404280(v=vs.110).aspx
ZipFile is contained in the assembly System.IO.Compression.FileSystem. (Thanks nateirvin...see comment below). You need to add a DLL reference to the framework assembly System.IO.Compression.FileSystem.dll
For .Net 4.5+
It is not always desired to write the uncompressed file to disk. As an ASP.Net developer, I would have to fiddle with permissions to grant rights for my application to write to the filesystem. By working with streams in memory, I can sidestep all that and read the files directly:
using (ZipArchive archive = new ZipArchive(postedZipStream))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
var stream = entry.Open();
//Do awesome stream stuff!!
}
}
Alternatively, you can still write the decompressed file out to disk by calling ExtractToFile():
using (ZipArchive archive = ZipFile.OpenRead(pathToZip))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
entry.ExtractToFile(Path.Combine(destination, entry.FullName));
}
}
To use the ZipArchive class, you will need to add a reference to the System.IO.Compression namespace and to System.IO.Compression.FileSystem.
We have used SharpZipLib successfully on many projects. I know it's a third party tool, but source code is included and could provide some insight if you chose to reinvent the wheel here.
Free, and no external DLL files. Everything is in one CS file. One download is just the CS file, another download is a very easy to understand example. Just tried it today and I can't believe how simple the setup was. It worked on first try, no errors, no nothing.
https://github.com/jaime-olivares/zipstorer
Use the DotNetZip library at http://www.codeplex.com/DotNetZip
class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files...
DotNetZip works on PCs with the full .NET Framework, and also runs on mobile devices that use the .NET Compact Framework. Create and read zip files in VB, C#, or any .NET language, or any scripting environment...
If all you want is a better DeflateStream or GZipStream class to replace the one that is built-into the .NET BCL, DotNetZip has that, too. DotNetZip's DeflateStream and GZipStream are available in a standalone assembly, based on a .NET port of Zlib. These streams support compression levels and deliver much better performance than the built-in classes. There is also a ZlibStream to complete the set (RFC 1950, 1951, 1952)...
String ZipPath = #"c:\my\data.zip";
String extractPath = #"d:\\myunzips";
ZipFile.ExtractToDirectory(ZipPath, extractPath);
To use the ZipFile class, you must add a reference to the System.IO.Compression.FileSystem assembly in your project
This will do it System.IO.Compression.ZipFile.ExtractToDirectory(ZipName, ExtractToPath)
Standard zip files normally use the deflate algorithm.
To extract files without using third party libraries use DeflateStream. You'll need a bit more information about the zip file archive format as Microsoft only provides the compression algorithm.
You may also try using zipfldr.dll. It is Microsoft's compression library (compressed folders from the Send to menu). It appears to be a com library but it's undocumented. You may be able to get it working for you through experimentation.
I use this to either zip or unzip multiple files. The Regex stuff is not required, but I use it to change the date stamp and remove unwanted underscores. I use the empty string in the Compress >> zipPath string to prefix something to all files if required. Also, I usually comment out either Compress() or Decompress() based on what I am doing.
using System;
using System.IO.Compression;
using System.IO;
using System.Text.RegularExpressions;
namespace ZipAndUnzip
{
class Program
{
static void Main(string[] args)
{
var directoryPath = new DirectoryInfo(#"C:\your_path\");
Compress(directoryPath);
Decompress(directoryPath);
}
public static void Compress(DirectoryInfo directoryPath)
{
foreach (DirectoryInfo directory in directoryPath.GetDirectories())
{
var path = directoryPath.FullName;
var newArchiveName = Regex.Replace(directory.Name, "[0-9]{8}", "20130913");
newArchiveName = Regex.Replace(newArchiveName, "[_]+", "_");
string startPath = path + directory.Name;
string zipPath = path + "" + newArchiveName + ".zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
}
}
public static void Decompress(DirectoryInfo directoryPath)
{
foreach (FileInfo file in directoryPath.GetFiles())
{
var path = directoryPath.FullName;
string zipPath = path + file.Name;
string extractPath = Regex.Replace(path + file.Name, ".zip", "");
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
}
}
You can do it all within .NET 3.5 using DeflateStream. The thing lacking in .NET 3.5 is the ability to process the file header sections that are used to organize the zipped files. PKWare has published this information, which you can use to process the zip file after you create the structures that are used. It is not particularly onerous, and it a good practice in tool building without using 3rd party code.
It isn't a one line answer, but it is completely doable if you are willing and able to take the time yourself. I wrote a class to do this in a couple of hours and what I got from that is the ability to zip and unzip files using .NET 3.5 only.
From here :
Compressed GZipStream objects written
to a file with an extension of .gz can
be decompressed using many common
compression tools; however, this class
does not inherently provide
functionality for adding files to or
extracting files from .zip archives.
I found out about this one (Unzip package on NuGet) today, since I ran into a hard bug in DotNetZip, and I realized there hasn't been really that much work done on DotNetZip for the last two years.
The Unzip package is lean, and it did the job for me - it didn't have the bug that DotNetZip had. Also, it was a reasonably small file, relying upon the Microsoft BCL for the actual decompression. I could easily make adjustments which I needed (to be able to keep track of the progress while decompressing). I recommend it.
From Embed Ressources:
using (Stream _pluginZipResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(programName + "." + "filename.zip"))
{
using (ZipArchive zip = new ZipArchive(_pluginZipResourceStream))
{
zip.ExtractToDirectory(Application.StartupPath);
}
}
Until now, I was using cmd processes in order to extract an .iso file, copy it into a temporary path from server and extracted on a usb stick. Recently I've found that this is working perfectly with .iso's that are less than 10Gb. For a iso like 29Gb this method gets stuck somehow.
public void ExtractArchive()
{
try
{
try
{
Directory.Delete(copyISOLocation.OutputPath, true);
}
catch (Exception e) when (e is IOException || e is UnauthorizedAccessException)
{
}
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
//stackoverflow
cmd.StartInfo.Arguments = "-R";
cmd.Disposed += (sender, args) => {
Console.WriteLine("CMD Process disposed");
};
cmd.Exited += (sender, args) => {
Console.WriteLine("CMD Process exited");
};
cmd.ErrorDataReceived += (sender, args) => {
Console.WriteLine("CMD Process error data received");
Console.WriteLine(args.Data);
};
cmd.OutputDataReceived += (sender, args) => {
Console.WriteLine("CMD Process Output data received");
Console.WriteLine(args.Data);
};
//stackoverflow
cmd.Start();
cmd.StandardInput.WriteLine("C:");
//Console.WriteLine(cmd.StandardOutput.Read());
cmd.StandardInput.Flush();
cmd.StandardInput.WriteLine("cd C:\\\"Program Files (x86)\"\\7-Zip\\");
//Console.WriteLine(cmd.StandardOutput.ReadToEnd());
cmd.StandardInput.Flush();
cmd.StandardInput.WriteLine(string.Format("7z.exe x -o{0} {1}", copyISOLocation.OutputPath, copyISOLocation.TempIsoPath));
//Console.WriteLine(cmd.StandardOutput.ReadToEnd());
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
Console.WriteLine(cmd.StandardError.ReadToEnd());
you can use Info-unzip command line cod.you only need to download unzip.exe from Info-unzip official website.
internal static void Unzip(string sorcefile)
{
try
{
AFolderFiles.AFolderFilesDelete.DeleteFolder(TempBackupFolder); // delete old folder
AFolderFiles.AFolderFilesCreate.CreateIfNotExist(TempBackupFolder); // delete old folder
//need to Command command also to export attributes to a excel file
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // window type
startInfo.FileName = UnzipExe;
startInfo.Arguments = sorcefile + " -d " + TempBackupFolder;
process.StartInfo = startInfo;
process.Start();
//string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Dispose();
process.Close();
}
catch (Exception ex){ throw ex; }
}

Read .xml file from project directory in Xamarin project

I have difficulty in reading the .xml file from Xamarin project. Whenever I debug my code I get below error
Could not find a part of the path "/storage/emulated/0/Xml/SupportedTypes.xml".
Below is the code what I am using
public static void Initialize(ConnectedDeviceCommType type)
{
string fileName = #"Xml\SupportedTypes.xml";
string filePath = Path.Combine(OS.Environment.ExternalStorageDirectory.ToString(), fileName);
try
{
var xml = XDocument.Load(filePath);
}
catch(Exception ex)
{
string st = ex.Message;
}
}
I have read that in some of the blogs that they say the .xml file needs to add to the Asset folder but in my case, I don't have any Asset folder in my project and The project I am trying to load is not an activity as well.
So can you please tell me is there any approach to read XML files directly in XAMARIN?
I finally made it work Thanks to Dimitris.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=windows
I don't really understand why you want to keep your xml file on your directrory. Because ,if you release your application, you probably won't set up the same directory structure as development. I suggest putting the files in the same folder as the application. Below the code can helps you to access your xml file from directory.
string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "SupportedTypes.xml");

How to get the files from the hierarchy of Zip Folder

I have a directory Structure like this:
A.zip -
A -
a -
1.dat
2.dat
I want to read the files 1.dat and 2.dat inside the directory hierarchy. I am able to read the file contentby C#, if the file is directly inside the zip folder but due to the inner directory structure is become inaccessible.
Any Help would be appreciated.
Thanks in advance.
Not sure how you are reading your zip file contents without an example, however reading zip file contents using the System.IO.Compression and System.IO.Compression.FileSystem assemblies is pretty simplistic. See the following example of how to read all files regardless of subdirectory within a zip file:
using System;
using System.IO.Compression;
namespace ZipReader
{
class Program
{
const string zipPath = #"D:\test\test.zip";
static void Main(string[] args)
{
using (var archive = ZipFile.OpenRead(zipPath))
{
foreach (var entry in archive.Entries)
{
Console.WriteLine(entry.FullName);
}
}
Console.ReadKey();
}
}
}
Produces the following output:
folder1/test1.txt
folder2/test2.txt
To get the contents you can simply call entry.Open() on each file which returns a Stream you can handle however you need to.

How to append a text file on SFTP server using SharpSSH or SSH.NET library

I use Tamir.SharpSSH library to make my SFTP operations. I can upload file from client, delete or list files located in an SFTP server directory.
But I cannot find how to append a text file. I don't want to overwrite or delete the existing and upload a new one. I have a log file on that SFTP server. I have to add new lines to that file from client side.
I just searched the internet and looked different functions in the code but tried nothing to execute because I could not find anything till now.
Thanks in advance
EDIT
I decided to use Renci.SshNet library because of #Martin Prikryl's advise. I tried the above operations with that library also and I saw it works good. Also appending text to a text file is very simple with that library. I'm sharing a small example about that here:
using System
using Renci.SshNet;
namespace SFTPConnectSample
{
class Program
{
static void Main(string[] args)
{
AppendText(#"/targetFolder/targetFile.txt");
}
private static void AppendText(string targetFilePath)
{
int portNumber = 22;
using (SftpClient sftp = new SftpClient("myHostName", portNumber, "sftpUser", "sftpPassword"))
{
sftp.ConnectionInfo.Timeout = new TimeSpan(0, 0, 30);
sftp.Connect();
sftp.AppendAllText(targetFilePath, "\r\nThis is a new line for target text file.");
}
}
}
}
First, do not use SharpSSH. It's an abandoned project, not updated for years.
You can use SSH.NET library instead.
Its SftpClient class has couple of methods you can use:
public StreamWriter AppendText(string path)
public void AppendAllText(string path, string contents)
public void AppendAllLines(string path, IEnumerable<string> contents)
If you must use SharpSSH for some reason, use:
SftpChannel.get(fromFilePath, toFilePath, monitor, ChannelSftp.APPEND);

How to unzip docx file using C#?

How to unzip docx file using C#?
The new Office file extensions (docx,potx,xlsx,etc) turning into zip files when they are uploaded to a web server and then downloaded.
These file formats are now using an Open XML file format system so they are more compatible with other office programs from Google, Open Office...etc. Essentially they are zip files that are full of XML files that when opened with a proper application turn into a friendly word document.
I stole this full of shame from here where you can find full info.
I hope this answer will help you and all the ignorant people that made fun of you and negative voting your question without even knowing the answer.
If you mean docx files, they're basically just zip files created with a particular convention.
Look into the Packaging API.
Here is the complete code that you are looking for. I have used this class for docx zip and unzip operations.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Deployment.Compression;
using Microsoft.Deployment.Compression.Zip;
namespace <YourPackage>.Libs
{
public class ZipFile
{
private string _zipfilepath;
public ZipFile(string zipfilepath)
{
_zipfilepath = zipfilepath;
}
public void Compress(string filePath,bool deleteSourceFolder)
{
var filePaths = new List<string>();
if (Directory.Exists(filePath))
{
filePaths.AddRange(Directory.GetFileSystemEntries(filePath).ToList());
}
if (filePaths.Count > 0)
{
var zip = new ZipInfo(_zipfilepath);
zip.Pack(filePath, true, CompressionLevel.None, null);
}
if(deleteSourceFolder)
Directory.Delete(filePath,deleteSourceFolder);
}
public void Uncompress(string destinationPath)
{
var zip = new ZipInfo(_zipfilepath);
zip.Unpack(destinationPath);
}
}
}
Set reference to System.IO.Compression and System.IO.Compression.FileSystem.
Then something like this:
using System.IO.Compression;
string zipPath = #"c:\tmp\Test.docx";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
archive.ExtractToDirectory(zipPath + ".unzipped");
}
Have a look here: https://msdn.microsoft.com/EN-US/library/hh485709(v=VS.110,d=hv.2).aspx (ZipFileExtensions.ExtractToDirectory Method)
You could try using System.IO.Packaging.ZipPackage.
Install the Open XML SDK http://www.microsoft.com/en-us/download/details.aspx?id=5124 and use this to work with the XML inside the Docx files.

Categories

Resources