I tried the zip.AddDirectory thing, but I can't figure out how to import a whole folder to an archive(Using DotNetZip).
I do NOT want this:
But this:
From the DotNetZip source code examples :
Remap directories. Zip up a set of files and directories, and re-map them into a different directory hierarchy in the zip file:
using (ZipFile zip = new ZipFile())
{
// files in the filesystem like MyDocuments\ProjectX\File1.txt , will be stored in the zip archive as backup\File1.txt
zip.AddDirectory(#"MyDocuments\ProjectX", "backup");
// files in the filesystem like MyMusic\Santana\OyeComoVa.mp3, will be stored in the zip archive as tunes\Santana\OyeComoVa.mp3
zip.AddDirectory("MyMusic", "tunes");
// The Readme.txt file in the filesystem will be stored in the zip archive as documents\Readme.txt
zip.AddDirectory("Readme.txt", "documents");
zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G") ;
zip.Save(ZipFileToCreate);
}
Related
The files I want to zip are in different folders
The folder structure is as follows:
FileId/FileType/File.extension
FileId/FileType/File.extension
I'm trying to create a zip with the following structure
FileType/File.extension
FileType/File.extension
I have tried a stackoverflow link but couldn't get it to work in my scenario.
I have the files in the zip directly. But I couldn't find a good source to have the files in a specific folder.
Code I have referred from previous posts:
using (ZipArchive archive = ZipFile.Open(zipPath + #"\release.zip", ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry;
DirectoryInfo d = new DirectoryInfo(folderToAdd);
FileInfo[] Files = d.GetFiles("*");
foreach (FileInfo file in Files)
{
archive.CreateEntry(preset.FileFormat);
readmeEntry = archive.CreateEntryFromFile(newFile, fileName);
}
}
I also tried creating a zip file in update mode and then as follows:
ZipFile.Open(zipPath + #"\release.zip", ZipArchiveMode.Update);
ZipFile.CreateFromDirectory(folderToAdd, zipPath + #"\release.zip", CompressionLevel.Fastest, true);
But the CreateFromDir breaks with an exception saying that the release.zip already exists.
Should I copy my files into respective folders and then apply the CreateFromDir method, or there is a better way to do this without re-copying and then CreateFromDir on top.
I am using DotNetZip library to compress files. my code is given below
String[] filenames = { "D:\\Data\\ReadMe.txt", "c:\\data\\collection.csv","c:\\ProgramFiles\\reports AnnualSummary.pdf"};
using (ZipFile zip = new ZipFile())
{
zip.AddFiles(filenames, "files");
zip.Save("Archive.zip");
}
Now when the Archive.zip is created, there are folders created in the archive with the same name(e.g Data, ProgramFiles etc)for files in which they were stored.
My target is to compress all the files in Archive.zip without any folder in the archive. how can I achieve it?
The AddFiles has an overload to flatten the directory structure in the zip archive
public void AddFiles(
IEnumerable<string> fileNames,
bool preserveDirHierarchy,
string directoryPathInArchive
)
http://dotnetzip.herobo.com/DNZHelp/html/c64e89d3-2f1f-2886-16bc-291746b46718.htm
zip.AddFiles(filenames, false, "files");
The second parameter preserveDirHierarchy can be passed as false to have all files in the root of the zip archive. Be careful that you do not have files with the same name when doing this. It will throw an Exception for duplicate file names.
I have the following code set up to create a zip file of a set of doucments:
public bool CreateDocumentationZipFile(int documentIdentifier, string zipDestinationPath, IList<string> documentPaths)
{
bool zipped = false;
if (documentPaths.Count > 0)
{
using (ZipFile loanZip = new ZipFile())
{
loanZip.AddFiles(documentPaths, false, zipDestinationPath);
loanZip.Save(string.Format("{0}{1}.zip",zipDestinationPath, documentIdentifier.ToString()));
zipped = true;
}
}
return zipped;
}
The issue I have is that when the zip file is created, the folder structure is maintaned within the zip file:
e.g
I am creating a zip of a selection of documents located at
C:\SoftwareDevelopment\Branches\ScannedDocuments\
When the created zip file is opened, there is a folder structure within the zip as follows:
Folder 1 ("SoftwareDevelopment")
Inside Folder 1 is folder 2 ("Branches")
Inside Folder 2 is folder 3 ("ScannedDocuments")
the scanned documents folder then contains the actual scan files.
Can anyone tell me how I can just have the scan files in the zip without the folders path being maintained?
The documentation states that the third parameter
directoryPathInArchive (String)
Specifies a directory path to use to override any path in the file
name. This path may, or may not, correspond to a real directory in the
current filesystem. If the files within the zip are later extracted,
this is the path used for the extracted file. Passing null (Nothing in
VB) will use the path on each of the fileNames, if any. Passing the
empty string ("") will insert the item at the root path within the
archive.
So if you always want to have the files added to the root of your zip archive, change
loanZip.AddFiles(documentPaths, false, zipDestinationPath);
to
loanZip.AddFiles(documentPaths, false, "");
say if I have the following:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("C:\\ReadMe.txt");
zip.AddFile("C:\\7440-N49th.png");
zip.AddFile("C:\\2008_Annual_Report.pdf");
zip.Save("C:\\Files\\ZipFiles\\Test.zip");
}
What I am confused on is that when I open up the zip file,
I it goes to C: first then I have to click on Files then I have to click on ZipFiles to see what is in the zip file.
Why doesn't it just open up the files I added when I click on the zip file?
are you using DotNetZip? and do you mean you want to add the files to the root of the zip file? If so, maybe you're trying to do
zip.AddFile("C:\\ReadMe.txt", "");
with the second string being the path you add it to in the zip(empty string if root).
I am using Ionic.zip to zip up the files.
You consider there are 2 files in c:\img\a.txt and b.txt. When zip up these files as following
using (ZipFile zip = new ZipFile())
{
zip.AddItem(#"F:\imp\a.txt");
zip.AddItem(#"F:\imp\b.txt");
zip.AddItem(#"F:\imp\lookup.ini");
zip.AddItem(#"F:\imp\lookups.mdb");
zip.Save("Lookups.zip");
}
It is creating lookups.zip file correctly. But the problem is content of zip file is...
there is one directory named as imp and it is contains those 2files.
But i do not need of the directory entry only i need files which i added.
Please help me.
As shown in the documentation, use the overload which accepts two strings - one for the directory in the zip file:
using (ZipFile zip = new ZipFile())
{
zip.AddItem(#"F:\imp\a.txt", "");
zip.AddItem(#"F:\imp\b.txt", "");
zip.AddItem(#"F:\imp\lookup.ini", "");
zip.AddItem(#"F:\imp\lookups.mdb", "");
zip.Save("Lookups.zip");
}
(That looks like 4 files to me, not 2, and in imp rather than img... but never mind.)