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!
Related
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.
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.
It is very perfect condition when I use as following code to compress English file or folder name,However I do have got serious problem when the name change from English to Chinese name , it isn't work.
How can i do ????
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(#"C:\inetpub\wwwroot\a"); // Zip folder included all branch files
zip.Save(#"C:\inetpub\wwwroot\Projectzip.zip");//location and name for creating zip file
}
I've found answer to deal with my problem
using (ZipFile zip = new ZipFile(System.Text.Encoding.Default))
thanks
I'm fetching an object from couchbase where one of the fields has a file. The file is zipped and then encoded in base64.
How would I be able to take this string and decompress it back to the original file?
Then, if I'm using ASP.MVC 4 - How would I send it back to the browser as a downloadable file?
The original file is being created on a Linux system and decoded on a Windows system (C#).
You should use Convert.FromBase64String to get the bytes, then decompress, and then use Controller.File to have the client download the file. To decompress, you need to open the zip file using some sort of ZIP library. .NET 4.5's built-in ZipArchive class should work. Or you could use another library, both SharpZipLib and DotNetZip support reading from streams.
public ActionResult MyAction()
{
string base64String = // get from Linux system
byte[] zipBytes = Convert.FromBase64String(base64String);
using (var zipStream = new MemoryStream(zipBytes))
using (var zipArchive = new ZipArchive(zipStream))
{
var entry = zipArchive.Entries.Single();
string mimeType = MimeMapping.GetMimeMapping(entry.Name);
using (var decompressedStream = entry.Open())
return File(decompressedStream, mimeType);
}
}
You'll also need the MIME type of the file, you can use MimeMapping.GetMimeMapping to help you get that for most common types.
I've used SharpZipLib successfully for this type of task in the past.
For an example that's very close to what you need to do have a look here.
Basically, the steps should be something like this:
you get the compressed input as a string from the database
create a MemoryStream and write the string to it
seek back to the beginning of the memory stream
use the MemoryStream as an input to the SharpZipLib ZipFile class
follow the example provided above to unpack the contents of the ZipFile
Update
If the string contains only the zipped contents of the file (not a full Zip archive) then you can simply use the GZipStream class in .NET to unzip the contents. You can find a sample here. But the initial steps are the same as above (get string from db, write to memory stream, feed memory stream as input to the GZipStream to decompress).
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.