Extracting an ISO to a folder using WinZip/WinRar - c#

I'm trying to extract an ISO using C#, I found a Winzip library, DotNetZip, and used that but when I run the project it says that it cannot extract the ISO.
string activeDir = copyTo = this.folderBD.SelectedPath;;
folderName = toExtract.Remove(toExtract.Length - 4, 4);
Path.Combine(activeDir, Path.GetFileNameWithoutExtension(folderName));
string zipToUnpack = toExtract;
string unpackDirectory = folderName;
using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
{
// here, we extract every entry, but we could extract conditionally
// based on entry name, size, date, checkbox status, etc.
foreach (ZipEntry file in zip1)
{
file.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
}
}
That is the code I am working with. copyTo and folderName are sent in from other methods in the program.
Any libraries that let me use Winzip or Winrar on a ISO would be a great help, but so far my searches have thrown up nothing.
Thanks in advance
EDIT:
Can you only extract .rar or .zip using winrar with C# or can you pass the file to be extracted as a arguement and how? I've tried
ProcessStartInfo startInfo = new ProcessStartInfo("winrar.exe");
Process.Start("winrar.exe",#"C:\file\to\be\extracted");
The ISO location, but that returns an exception that there is nothing to extract there.

You can execute winrar from c# using Process.Start and pass in the arguments you need to extract the iso.

Related

Unzip having file name non utf-8 is not extracting in correct name

I am extracting a zip file using system.io.compression in .net 4.5
var path = #"<zipfilePath>";
using (ZipArchive zarc = ZipFile.Open(path, ZipArchiveMode.Read,Encoding.UTF8))
{
var file= zarc.Entries.First().FullName;
}
電子メール・テンプレート___第_6_世代インテル®_コア™_ヴィープロ™プロセッサー(カスタマイズ可能
this is my file in the zip.
the filename after extracting becomes
pchir�gzxsii___u_6_wtyzgrr_nwT_axixtT_xtvdpi_(ftzeyulb
I know its a encoding issue.But I am not sure which encoding to use here.
Additionally I would like to know if a zip contains filename like chinese,korean etc . how to handle encoding for each so that after extracting those I get the exact file name.
Thanks in advance.

Zip a file which name contains non-ASCII character, using C#

I am trying to zip a file using C# (.net 4.6). Here is my code:
string targetLoc = #"C:\path\to\target\zip";
string sourceFile = #"C:\path\to\source\file\ファイル名.csv";
ZipArchive zip = ZipFile.Open(Path.Combine(targetLoc, "ZipFile.zip"), ZipArchiveMode.Create, Encoding.UTF8);
using (zip)
{
string fileName = Path.GetFileName(sourceFile);
zip.CreateEntryFromFile(sourceFile, fileName);
}
The problem is, the file name contained in the resulted zip file became this: 繝輔ぃ繧、繝ォ蜷・csv. I tried changing the encoding of the zip (in the ZipArchive zip = ZipFile.Open(Path.Combine(targetLoc, "ZipFile.zip"), ZipArchiveMode.Update, Encoding.UTF8);), but I got ArgumentException saying that the encoding is not supported.
Is there any workaround for this? As much as possible I do not want to use third party library.
note1: the zip file did not previously exist.
note2: I am using win7 Pro, Japanese version (not possible to change language)
Thank you in advance!

Opening Split Zip Files With DotNetZip

I am trying to extract files from zip files using the DotNetZip library. I am able to extract files when it is a single .zip file. However, when I try to extract files from a multi volume zip file like Something.zip.0 or Something.zip.1, I get the following two exceptions:
-Exception thrown: 'Ionic.Zip.BadReadException' in Ionic.Zip.dll
-Exception thrown: 'Ionic.Zip.ZipException' in Ionic.Zip.dll
Is it possible for DotNetZip to read these type of files, or should I be looking into an alternative approach? I am working on Visual Studios using C#.
Here's a snippet of how I implement my zip file extraction.
using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(_pathToZip))
{
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
foreach(Ionic.Zip.ZipEntry ze in zip)
{
string fileName = ze.FileName;
bool isThereItemToExtract = isThereMatch(fileName.ToLower(), _folderList, _fileList);
if (isThereItemToExtract)
{
string pathOfFileToExtract = (_destinationPath + "\\" + ze.FileName).Replace('/', '\\');
string pathInNewZipFile = goUpOneDirectoryRelative(ze.FileName);
ze.Extract(_destinationPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);
_newZip.AddItem(pathOfFileToExtract, pathInNewZipFile);
}
}
_newZip.Save();
}
Please refer the DotNetZipLibrary code examples:
using Ionic.Zip;
private void MyExtract(string zipToUnpack, string unpackDirectory)
{
using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
{
// here, we extract every entry, but we could extract conditionally
// based on entry name, size, date, checkbox status, etc.
foreach (ZipEntry e in zip1)
{
e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
}
}
}
This method should be able to extract either split and not split zip files.
Every zip entry will be extracted with its full path as specified in the zip archive, relative to the current unpackDirectory.
There's no need to check if zip entry exsists (isThereItemToExtract). Interating the zip entries with foreach should do the job.
To avoid collisions you need to check if file with same name as zipEntry exsists in the unpackDirectory, or use ExtractExistingFileAction.OverwriteSilently flag.
Is it possible for DotNetZip to read these type of files, or should I be looking into an alternative approach? I am working on Visual Studios using C#.
In my experience, this is the best library to deal with split zip files.

c# extract zip file with directory

I'm using c# fw4.5.
I have a simple code extracting a zip file.
foreach(ZipArchiveEntry entry in z.entries) //z is a zip file open in ZipArchiveMode.Read
{
entry.ExtractToFile(entry.FullName);
}
The zip file have a directory inside it and all files are inside that directory.
When I look at the z.Entries I see its an array which place [0] is only the directory and [1],[2],[3] are files.
But when its try to do:
entry.ExtractToFile(entry.FullName);
On the first entry, I get an error:
"The filename, directory name or volume label syntax is incorrect".
I can't seems to find out whats wrong. Do I need to anything also for it to open the directory? Maybe because the entry is a directory only the "ExtractToFile(entry.FullName)" can't work?
Thanks in advanced.
According to this MSDN article, the ExtractToFile method expects a path to a file (with an extension) and will throw an ArgumentException if a directory is specified.
Since the first entry in the archive is a directory and you are using its name as the argument, that is why you are having this issue.
Look into the related ExtractToDirectory method, which is used like so:
ZipFile.ExtractToDirectory(#"c:\zip\archive.zip", #"c:\extract\");
In addition to Tonkleton's answer I would suggest that you use a third-party compression library since ZipArchive is not supported for framework versions before the .Net 4.5 framework, might I suggest DotNetZip as mentioned in other questions regarding compression in earlier frameworks on StackOverflow.
Replace your paths:
void Main()
{
var zipPath = #"\\ai-vmdc1\RedirectedFolders\jlambert\Downloads\cscie33chap1and2.zip";
var extractPath = #"c:\Temp\extract";
using (ZipArchive z = ZipFile.OpenRead(zipPath))
{
foreach(ZipArchiveEntry entry in z.Entries) //z is a zip file open in ZipArchiveMode.Read
{
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName), true);
}
}
}

C# SharpZipLib extract .TGZ NotSupportedException

I'm new to programming so please be patient.
I am currently developing a small Program in Visual C# 2010 Express, .NET Framework 4.0, which starts a Script on a Linux Machine (what creates the File /tmp/logs.tgz), downloads the File and then I want to extract it. Running the Script and downloading the File via Renci.SshNet works flawlessly.
But when I want to extract it, it gives me an Error "NotSupportedException" my Filepath Format is incorrect (which is not the case, I think?).
I copy and pasted the Code directly from here (Simple full extract from a TGZ (.tar.gz)) and edited it for my Needs:
using System.IO;
using System.IO.Compression;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
//it executed the Script and created the file on the Linux Machine /tmp/logs.tgz
//now I want to download it
string myTime = DateTime.Now.ToString("yyyyMMdd");
var pathWithEnv = (#"%USERPROFILE%\Desktop\logs" + myTime + ".tgz");
var filePath = Environment.ExpandEnvironmentVariables(pathWithEnv);
string localFile = filePath;
//then downloads /tmp/logs.tgz to ..\Desktop\logs+ myTime +.tgz
//everything great until now. here I want to extract .TGZ:
var pathWithEnv2 = (#"%USERPROFILE%\Desktop\logs" + myTime);
var fileDir = Environment.ExpandEnvironmentVariables(pathWithEnv2);
string localDir = fileDir;
Stream inStream = File.OpenRead(localFile);
Stream gzipStream = new GZipInputStream(inStream);
TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
//ERROR OCCURS HERE:
tarArchive.ExtractContents(localDir);
tarArchive.Close();
gzipStream.Close();
inStream.Close();
I even tried to set the localFile and localDir string without the EnviromentVariable, but that didnt help. I tried:
- download and extract it directly on C:\ (or on a mapped Network Drive U:) to prevent too long filenames (which should not be the case as it should never get longer than 86 characters).
- string = #"C:..\logs", string = "C:\..\logs", string = #"C:..\logs\", etc.
- tried it without myTime
- using ICSharpCode.SharpZipLib.Core;
I did a MessageBox.Show(localDir); before the tarArchive.ExtractContents(localDir); and it showed "C:\Users\Baumann\Desktop\logs20140530" which is correct, thats the Directory I want to extract it to. Even creating the Directory before executing it doesn't help.
I also created a new Project with just one button which should start the Extraction and the same error occurs.
I tried, doing it separately, first extract the GZip and then the .tar, but it also gives me the same Error when extracting the GZip (using ICSharpCode.SharpZipLib.Core; of course).
What drives me even more crazy about it, is, that it starts to extract it, but not everything, before it fails. And always the same Files, whats not clear for me why these and why not the others.
I'm on Windows 8.1, using SharpZipLib 0.86.0.518, downloaded directly from the Website.
Thanks in advance.
well, I finally fixed the Problem. The Linux machine is creating a file which includes the MAC-Adress and since Windows can't handle ":" in a Filename, it crashes.
I am now extracting file by file and checking each file for ":" and replacing it with "_", works flawlessly.

Categories