Adding files into a folder inside a zip file in c# - c#

My code to zip files is as follows
ZipArchive zip = ZipFile.Open(destToZip, ZipArchiveMode.Create);
zip.CreateEntry("pubEd/");
string[] fileEntries = Directory.GetFiles(dirToZip);
foreach (string fileName in fileEntries)
zip.CreateEntryFromFile(fileName,Path.GetFileName(fileName), CompressionLevel.Optimal);
zip.Dispose();
In the second line of the code once after creating a zip file I create a folder with a name pubEd inside the zip file.
In the next line I am adding files to the zip folder.
What is happening is files get added to the zip directly.
I want to add these files inside the directory which I created inside the zip.
How do I do that?

By the looks of it you would changePath.GetFileName(fileName) to "pubEd/" + Path.GetFileName(fileName). And get rid of the second line. Thats just based on my reading of the documentation. I have not actually tried it.

Related

Create a directory in a zipFile and add files using C#

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.

Creating a zip file with ionic.zip

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, "");

Read all text files in a folder with StreamReader

I am trying to read all .txt files in a folder using stream reader. I have this now and it works fine for one file but I need to read all files in the folder. This is what I have so far. Any suggestions would be greatly appreciated.
using (var reader = new StreamReader(File.OpenRead(#"C:\ftp\inbox\test.txt")))
You can use Directory.EnumerateFiles() method instead of.
Returns an enumerable collection of file names that match a search
pattern in a specified path.
var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt");
foreach (string currentFile in txtFiles)
{
...
}
You can call Directory.EnumerateFiles() to find all files in a folder.
You can retrieve the files of a directory:
string[] filePaths = Directory.GetFiles(#"c:\MyDir\");
Therefore you can iterate each file performing whatever you want. Ex: reading all lines.
And also you can use a file mask as a second argument for the GetFiles method.
Edit:
Inside this post you can see the difference between EnumerateFiles and GetFiles.
What is the difference between Directory.EnumerateFiles vs Directory.GetFiles?

Zip files using ZipFiles

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).

SharpZipLib - adding folders/directories to a zip archive

From examples, I've got a pretty good grasp over how to extract a zip file.
In nearly every example, the method of identifying when a ZipEntry is a directory is as follows
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > 0)
Directory.CreateDirectory(Path.Combine(destinationDirectory, directoryName));
if (fileName != String.Empty)
{
//read data and write to file
}
Now is is fine and all (directory encountered, create it), directory is available when the file is extracted.
I can add files to a zip fine, but how do I add folders? I understand I'll be looping through the directories, adding the files encountered (and their ZipEntry.Name property is populated properly), but how do I add a ZipEntry to the archive and instruct the ZipOutputStream that it is a directory?
ZipFile.AddDirectory does what you want. Small sample code here.

Categories