I need to combine 3 files into 1 zip file and make it available to download for the user. I am able to achieve my requirement except one thing: it zips the files into the subfolders.
For example, my files are located like the following:
C:\TTCG\WebSites\Health\ABC.CSV
C:\TTCG\WebSites\Health\XYZ.CSV
C:\TTCG\WebSites\Health\123.CSV
But in the zip file, it zip the files in the folder by using "TTCG\WebSites\Health\" as the path. Please see the attach file.
I don't want the folders in the path. I just want 3 files in the zip file without folders. How can I achieve that?
My codes to generate the zip file is as below:
ZipFile z = ZipFile.Create(Server.MapPath("~" + #"\Accident.zip"));
//initialize the file so that it can accept updates
z.BeginUpdate();
//add the file to the zip file
z.Add(Server.MapPath("~" + #"\ABC.csv"));
z.Add(Server.MapPath("~" + #"\XYZ.csv"));
z.Add(Server.MapPath("~" + #"\123.csv"));
//commit the update once we are done
z.CommitUpdate();
//close the file
z.Close();
Based on the FAQ, you have to strip the folder path out manually:
How can I create a Zip file without folders?
Remove the path portion of the filename used to create a ZipEntry
before it is added to a ZipOutputStream
ZipEntry entry = new ZipEntry(Path.GetFileName(fullPath));
The FAQ can be found here.
It seems to be a limitation of the library. Hope this helps!
If you have your files in a FileSystemInfo, you can use: z.Add(file.FullName, Path.GetFileName(file.FullName));
This will add your files in the root directory of your zip.
z.Add(pathToFile, pathInZipFile);
Related
I am using ZIpstorer class library to compress files. I am able to zip a file by calling zip.AddFile procedure. But I want to zip folders which has subfolders too. So the resulting zip file should have folder and subfolder structure inside the zip file normally. I am not able to do it. if i process each file by looping all folders and sub folders and then if call zip.Addfile then it will result in zip file with all the files inside it without a directory structure.
So how can i zip a folder using Zipstorer class . What changes i have to do?
You can use (back)slashes for the _filenameInZip (sic) parameter to add files in a directory in the zip:
zip.Addfile(,,"directory/filename.txt",);
Or
zip.Addfile(,,"directory\\filename.txt",);
How can I create a file even if the some sub-directories dont exist? I am aware of this Question and its answer that suggests I use Directory.CreateDirectory(path) to create both my file and its directories.
But when I attempt to create my file using Directory.CreateDirectory(path) it creates the file as a directory/folder and not as a file?
Directory.CreateDirectory (#"C:\Users\me\AppData\LocalLow\company\product\database.db");
Why is database.db a folder and not a file? Am I doing something wrong? How can I create a file even if the some sub-directories dont exist?
You need to specify the directory path to the CreateDirectory method, not the full file path. You can extract this from the full path using GetDirectoryName:
string filePath = #"C:\Users\me\AppData\LocalLow\company\product\database.db";
string dirPath = Path.GetDirectoryName(filePath);
// gives #"C:\Users\me\AppData\LocalLow\company\product"
Directory.CreateDirectory(dirPath);
File.Create(filePath); // can use any file API call here, such as File.Open
Is there a way to ONLY zip files and directories at the same time. I was using ZipArchive but is not working for Directories. The directory that I want to add in the Zip file has more directories and a zip file inside, so it's a nested directory. Can someone please assist.
using (ZipArchive zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
{
foreach (FileInfo file in filesToZip)
{
zipArchive.CreateEntryFromFile(Path.Combine(filePath, file.Name), file.Name);
}
}
}
Have you tried ZipFile.CreateFromDirectory?
ZipFile.CreateFromDirectory(filePath, zipFilePath);
If you need to zip up an entire directory (including sub-folders), this may be simpler.
To create an empty file or directory, use CreateEntry(String)
Include the slash for directories
zipArchive.CreateEntry(#"DirectoryName\");
I have a very large zip file which contains other zip files inside of it. I want my c# program to be able to recognize that the file is a zip file and if it is a zip file, then to extract it to a folder in the same location as the zip file. My code is here:
private void Unzip(OpenFileDialog tvZipOpen)
{
string zipFile = tvZipOpen.FileName; // file to unzip
int i = zipFile.LastIndexOf(".zip");
string targetDirectory = zipFile.Substring(0, i); // location to extract to
using (ZipArchive zip = ZipFile.OpenRead(zipFile))
{
zip.ExtractToDirectory(targetDirectory);
}
tvZipOpen.InitialDirectory = targetDirectory;
tvZipOpen.ShowDialog();
}
I am using the ZipFile class from .NET 4.5 and i call on this method here:
if (tvOpen.ShowDialog() == DialogResult.OK)
{
while (tvOpen.FileName.ToLower().EndsWith(".zip"))
{
Unzip(tvOpen);
}
return tvOpen.FileNames;
}
The code works fine for extracting the first zip file but when I try to extract the second zip file, I get an InvalidDataException that says local file header is corrupt. However, I don't think it is corrupt because I am able to open and extract the zip files perfectly in windows explorer. I'm not sure if the fact that it is a large zip file with a zip64 extension has anything to do with it but whatever the problem is, how come I don't get the problem when I open and extract in windows explorer and how do I fix this? Any help would be greatly appreciated.
c# does not support the .zip64 extension.
how large is your zip file because if it under 4GiB rename it to .zip and it should work fine if it is larger than that see this
http://dotnetzip.codeplex.com/
To change the file extension
Open windows explorer and press Alt + V
Then go to tools and then folder options and make sure that the hide extensions for known file types box is unchecked and click apply and ok.
then simply rename the file to remove the 64 from the extension so it is just .zip
Then Click yes on the prompt
And then you should be able to open the file in your program
hope this helps
i have a folder structure and files in those folders.i have to zip all those files with folder structure.
i found examples which only zip a file not all folder structure. i am working on VS 2008. please suggest me the way to zip files with folder structure with c#
Have you tried DotNetZip ?
You can use it to zip a folder.
actually i dont have to create folders first. it creates folder automatically when i specify files with folder name. and it works with both the links provided. thanks guys