Ionic.Zip Splitt up zip can't be extracted - c#

I zip a directory with Ionic.Zip and splitt it up into several files. The result is a bunch of files named myfile.zip, myfile.z01, myfile.z02,...
When I look into the zip-File with windows explorer, I can see the file list.
But when I try to extract the archive with windows explorer, I get a message The same volume can not be used as both the source and destination
When I open the zipfile with 7zip I get the message file myfile.zip cannot be opened as archive
Creating and extracting a single zip-archive works fine.
Here is the code, where I create the zip archive, using Ionic.Zip
using (ZipFile zip = new ZipFile())
{
//zip.AlternateEncoding = System.Text.Encoding.UTF8;
zip.AddDirectory(sourceDirectory);
//zip.MaxOutputSegmentSize = 0; //Single file
zip.MaxOutputSegmentSize = 1024 * 1024 * 8; //Splitt up into 8 MB pieces
//zip.Password = zipPassword;
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.None;
zip.CompressionMethod = CompressionMethod.None;
zip.Save(zipFilePath);
segmentsCreated = zip.NumberOfSegmentsForMostRecentSave;
}
return segmentsCreated;
Btw, I tried several combinations of CompressionLevels, CompressionMethods, with and without password,... No changes :(
UPDATE 1:
Unpacking works:
using (ZipFile zip = ZipFile.Read(zipFilePath))
{
zip.Password = zipPassword;
zip.ExtractAll(targetDirectory, ExtractExistingFileAction.OverwriteSilently);
}

This is a known issue.
you can't open a partial zip file using the windows explorer.
Use WinRar.

Related

Zip files without inclusion of folders

I'm using System.IO.Compression in order to compress a file into a .zip, below the source code:
using (FileStream zipToOpen = new FileStream(zipName, FileMode.CreateNew)){
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)){
ZipArchiveEntry readmeEntry = archive.CreateEntry(#"C:\Users\soc\myFold\someFile.xml");
}
}
This piece of code works well, but unfortunately in the .zip there's the entire sequence of folders (C: -> Users -> ... -> someFile.xml); can I obtain a final .zip with ONLY the file I need? I know that with other libraries this fact is possible (DotNetZip add files without creating folders), but I would like to know if it were possible do the same with the standard library.
You seem to be under the impression that the file will be added to the archive, which is not the case. CreateEntry merely creates an entry with the specified path and entry name, you still need to write the actual file.
In fact, the code in your question is quite similar to the code in the documentation, so I assume you got it from there?
Anyway, to get only the file name you can use Path.GetFileName and then you can write the actual file content to the zip entry.
var filePath = #"C:\temp\foo.txt";
var zipName = #"C:\temp\foo.zip";
using (FileStream zipToOpen = new FileStream(zipName, FileMode.CreateNew))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry = archive.CreateEntry(Path.GetFileName(filePath));
using (StreamReader reader = new StreamReader(filePath))
using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
{
writer.Write(reader.ReadToEnd());
}
}
}
The code above will create an archive with foo.txt in the root and with the content of the source file, without any additional directories.

Zip files in a folder using c# and return response

I want to zip files and folders in a folder using c# and I've checked previously asked questions but they don't work for me...i'm currently trying to work with DotNetZip. Here's a bit of my code:
using (ZipFile zip = new ZipFile())
{
string[] files = Directory.GetFiles(#"C:\Users\T1132\Desktop\logs");
// add all those files to the logs folder in the zip file
zip.AddFiles(files, "logs");
zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
var a = System.DateTime.Now.ToString();
zip.Save(#"C:\Users\T1132\Desktop\logs\Archiver.zip");
foreach (var f in files)
{
System.IO.File.Delete(f);
System.Diagnostics.Debug.WriteLine(f + "will be deleted");
}
}
the code above works but it only zips the files and leaves the folders. Kindly assist me please, Thanks.
There are so many ways to zip files using DotNetZip.
using (ZipFile zip = new ZipFile())
{
zip.Encoding = System.Text.Encoding.GetEncoding("big5"); // chinese
zip.AddDirectory(#"MyDocuments\ProjectX");
zip.Save(ZipFileToCreate);
}
Above zips a directory. For more uses you can follow the links
https://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples
or
http://grapevine.dyndns-ip.com/download/authentec/download/dotnetzip/examples.htm
Thanks
I am using this and it is working great. Make sure directory has enough permission(s).
System.IO.Compression.ZipFile.CreateFromDirectory(zipPath, path + strFileName + ".zip");
You can use below code.
string zipFileName= "zip full path with extension";
var files = Directory.GetFiles(" directory path");
Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile();
foreach(var file in files) {
zip.AddFile(file);
}
zip.Save(zipFileName);

DotNetZip uncompressed exe files not opening

I'v been using the DotNetZip library for uncompressing some files from a ZIP file, compressed using the same library. That ZIP file contains exe files, but once decompressed, some of the exe file decompressed is not opening anymore. That problem is not happening if i decompress the file manually with WinRAR.
The code i'm using is this one...
if (!System.IO.File.Exists("ApplicationSample.zip")) { MessageBox.Show("No data found", "Warning"); return false; }
var Archive = ZipFile.Open(DatFilePath, ZipArchiveMode.Read);
var InstallPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + #"/ApplicationSample";
if (Directory.Exists(InstallPath))
Directory.Delete(InstallPath, true);
Directory.CreateDirectory(InstallPath);
Archive.ExtractToDirectory(InstallPath);
var ArchiveEd = new Ionic.Zip.ZipFile("ApplicationSample.zip");
ArchiveEd.UseUnicodeAsNecessary = true;
ArchiveEd.ZipError += ApplicationSample_ZipError;
ArchiveEd.ExtractAll(InstallPath);
ArchiveEd.Dispose();
ShellLink link = new ShellLink();
link.WorkingDirectory = InstallPath;
link.Target = InstallPath + "/ApplicationSample.exe";
link.IconPath = InstallPath + "/ApplicationSample.ico";
link.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/ApplicationSample.lnk");
The exe file i'm trying to open is an WPF application that works correctly if i do the same operation manually.
I don't know what's happening here and i would like to know what's the problem.
Thanks!!

OutOfMemory exception when trying to download multiple files as a Zip file using Ionic.Zip dll

This is my working code that I used to download multiple files as a zip file using Ionic.Zip dll. File contents is stored in a SQL database. This program works if I try to download 1-2 files at a time, but throws an OutOfMemory exception if I try to download multiple files as some of the files may very large.
Exception occurs when it's trying to write in to outputStream.
How can I improve this code to download multiple files or is there a better way to download multiple files one by one rather than zipping them to a one large file?
Code:
public ActionResult DownloadMultipleFiles()
{
string connectionString = "MY DB CONNECTIOBN STRING";
List<Document> documents = new List<Document>();
var query = "MY LIST OF FILES - FILE METADA DATA LIKE FILEID, FILENAME";
documents = query.Query<Document>(connectionString1).ToList();
List<Document> DOCS = documents.GetRange(0, 50); // 50 FILES
Response.Clear();
var outputStream = new MemoryStream();
using (var zip = new ZipFile())
{
foreach (var doc in DOCS)
{
Stream stream = new MemoryStream();
byte[] content = GetFileContent(doc.FileContentId); // This method returns file content
stream.Write(content, 0, content.Length);
zip.UseZip64WhenSaving = Zip64Option.AsNecessary // edited
zip.AddEntry(doc.FileName, content);
}
zip.Save(outputStream);
}
return File(outputStream, "application/zip", "allFiles.zip");
}
Download the files to disc instead of to memory, then use Ionic to zip them from disc. This means you don't need to have all the files in memory at once.

DotNetZip How Add Selected Files without creating folders

I want to add in zip file "test" all pdf files from path
using (var zip = new ZipFile())
{
zip.AddSelectedFiles("*.pdf",path);
zip.Save(path+"/test.zip");
}
when test.zip file is created have this directory :
**test.zip**\Users\administrator\Documents\vs2010\Projects\my project\**pdf files**
How to make all pdf documents to be directly in test.zip
test.zip\pdf files
Please try the following,
using (ZipFile zip = new ZipFile())
{
string[] files = Directory.GetFiles(path);
// filter the files for *.pdf
zip.AddFiles(files, "Test"); //Test Folder
zip.Save(path+"/test.zip");
}

Categories