I create a zip File using "UseZip64WhenSaving" option as true.
Once I try to extract it with "ExtractAll" method, an exception is thrown saying that the file "nameOfMyFile.z65536" does not exist.
It does not happen with files created as normal zip (not zip64).
Any suggestion to solve the issue?
The code creating the file:
using (ZipFile zCompressor = new ZipFile(strNameOftheZipFile))
{
zCompressor.UseZip64WhenSaving = Zip64Option.Always;
FileInfo[] fiArrayFiles = dInfoBCP.GetFiles("*.bcp", SearchOption.TopDirectoryOnly);
foreach (FileInfo fileTemp in fiArrayFiles)
{
zCompressor.AddFile(fileTemp.FullName);
}
zCompressor.MaxOutputSegmentSize = (700 * 984540);
zCompressor.Save();
}
The code to extract (that generates the error):
ZipFile zip = ZipFile.Read(FullNameOfmyZipFile);
zip.ExtractAll(strPathDest, ExtractExistingFileAction.OverwriteSilently);
Related
I have files stored in multiple different folders and I need to make a ZIP archive from all the files in those folders. I have created a simple function using System.IO.Compression, that takes the data from just one folder and makes a ZIP archive, but I can't figure out how to do that for multiple folders. No folders needed in ZIP, just the files from it.
If it can't be done in this library, I can use a different one like DotNetZip or similar.
string folder1 = #"c:\ex\ZipFolder1";
string zipPath = #"c:\ex\AllFiles.zip";
ZipFile.CreateFromDirectory(folder1, zipPath);
I think you're using DotNetZip.Just create a ZipFile and add files by the method AddFiles
using (var file = new ZipFile())
{
//fileNames is an array containing the paths of the files from differents folders
file.AddFiles(fileNames);
file.Save(zipFile);
}
Have you tried using AddFile() method of ZipFile Class. Here you can replace 'PathOfFiles' dynamically as per your requirement.
var fileNames = new string[] { "a.txt", "b.xlsx", "c.png" };
using (var zip = new ZipFile()){
foreach (var file in fileNames)
zip.AddFile(#"PathOfFiles\" + fileName, "");
zip.Name = "ZipFile";
var pushStreamContent = new PushStreamContent((stream, content, context) =>
{
zip.Save(stream);
stream.Close();
}, "application/zip");
}
for anyone encountering this today, here's an updated short and simple answer based on Microsoft's docs:
https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zipfileextensions.createentryfromfile?view=net-6.0
static void SaveFilesToZip(string zipTargetPath, string[] filePaths)
{
using var newZip = ZipFile.Open(zipTargetPath, ZipArchiveMode.Create);
foreach (var filePath in filePaths)
{
newZip.CreateEntryFromFile(filePath, Path.GetFileName(filePath));
}
}
I have a number of folders and I have number of file in each folders.
I am taking some of the file paths from different folders and I need to zip those files into a single zip file.
if I have the file in a single folder, I can do zip using
string startPath = #"c:\example\start";//folder to add
string zipPath = #"c:\example\result.zip";//URL for your ZIP file
ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
string extractPath = #"c:\example\extract";//path to extract
ZipFile.ExtractToDirectory(zipPath, extractPath);
using (ZipArchive newFile = ZipFile.Open(zipName, ZipArchiveMode.Create))
{
foreach (string file in Directory.GetFiles(myPath))
{
newFile.CreateEntryFromFile(file);
}
}
Suppose I don't know about the folders, I only have a list of file paths, I need to foreach through each file path and add into the zip. What should I do for this?
You can enumerate all files in the directory include subdirectories with Directory.GetFiles overloading and SearchOption.AllDirectories option and then use it
String[] allfiles = System.IO.Directory.GetFiles("myPath", "*.*", System.IO.SearchOption.AllDirectories);
foreach (string file in allfiles)
{
newFile.CreateEntryFromFile(file);
}
You can use ZipFile.CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName) or its overload to create an archive in one step too.
Also there is a way to manipulate zip archive structure more flexibly via Path.GetDirectoryName() method. Here is the example:
foreach(var filePath in files)
{
var directory = Path.GetDirectoryName(filePath);
var fileName = Path.GetFileName(filePath);
var entry = archive.GetEntryFromFile(filePath, $"{directory}/{fileName}");
}
Finally, you can use 3rd party library DotNetZip and solve yor scenario in just 3 lines of code:
using (ZipFile zip = new ZipFile())
{
zip.AddFiles(files);
zip.Save("Archive.zip");
}
Using the DotNetZip library from newget manager, just check whether it will work or not
using (ZipFile zip = new ZipFile())
{
foreach(var filestring in AllFileArray)
{
zip.AddFile(filestring);
}
zip.save("MyZipFile.zip")
}
I need to zip a folder that contains folders, common files and zipfiles. But I need to except the files with the "zip" extension. Also I need to create the zipfile or update it if is already created.
In order to do this, I check all files and directories and then add the files to the zip file. So far so good, but I can't find a way to add or update a folder.
This is my last tried:
var files = Directory.GetFiles(directoryToCompress).Where(name => !name.EndsWith(".zip", true, System.Globalization.CultureInfo.CurrentCulture));
var directories = Directory.GetDirectories(directoryToCompress);
if (File.Exists(filenameZip))
{
using (ZipArchive zipDest = ZipFile.Open(filenameZip,ZipArchiveMode.Update))
{
foreach (var file in files)
{
zipDest.
zipDest.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
System.IO.File.Delete(file);
}
}
}
else
{
using (ZipArchive zip = ZipFile.Open(filenameZip, ZipArchiveMode.Create))
{
foreach (var file in files)
{
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
System.IO.File.Delete(file);
}
}
}
Thanks in advance for any tip or help
I am using .NET 2.0 and Linq is out of question. I would like to check if file exists inside a directory without knowledge of the file extension.
I just need this logic done.
1.Check File exists in Directory using String Filename provided using search pattern leaving out the extension of the File
2.Get the Files if they exists and Databind to provide Download links.If file does not exist then start uploading the File.
Update:
Directory.GetFiles() and DirectoryInfo.GetFiles() indeed solves the part where in i check for File existence. As for the performance regarding FileInfo objects, these were only solution to my requirements of databinding to provide download links
DirectoryInfo root = new DirectoryInfo("your_directory_path");
FileInfo[] listfiles = root.GetFiles("dummy.*");
if (listfiles.Length > 0)
{
//File exists
foreach (FileInfo file in listfiles)
{
//Get Filename and link download here
}
}
else
{
//File does not exist
//Upload
}
Hope this works
To see if a file exists with that name, can you not just use..
However, Directory.GetFiles already includes the full path
string [] files = Directory.GetFiles(Path,"name*");
bool exists = files.Length > 0;
if ( exists)
{
//Get file info - assuming only one file here..
FileInfo fi = new FileInfo(files[0]);
//Or loop through all files
foreach (string s in files)
{
FileInfo fi = new FileInfo(s);
//Do something with fileinfo
}
}
You can use DirectoryInfo.GetFiles() to have a FileInfo[] instead of a String[].
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");
}