ZipStorer how add directory? - c#

static string mydir = #"C:\Boba\bin\Release\ZipTest";
static string zipfile = string.Concat(mydir, ".zip");
using (ZipStorer zip = ZipStorer.Create(zipfile))
{
zip.AddDirectory(ZipStorer.Compression.Deflate, mydir, zipfile);
}
But after I unpack the archive, folders appear
Dir: Boba -> bin > Release > ZipTest > Files...
How do I add only the ZipTest folder?
I tried to do it like this:
DirectoryInfo d = new DirectoryInfo(mydir);
zip.AddDirectory(ZipStorer.Compression.Deflate, Path.GetFileName(d.FullName), Path.GetFileName(d.FullName), comment);
A Zip archive is created, and inside a folder called ZipTestZipTest, inside there are files and an empty archive called .zip.
How to make it just ZipTest inside the archive?
And so that there is no empty archive in the ZipTest folder?

Try set 3rd argument (_pathnameInZip) for .AddDirectory as empty string:
string dir = #"C:\Boba\bin\Release\ZipTest";
string zipFile = string.Concat(dir, ".zip");
string comment = "My ZipTest";
using (ZipStorer zip = ZipStorer.Create(zipFile))
{
zip.AddDirectory(ZipStorer.Compression.Deflate, dir, string.Empty, comment);
}

If you need without an internal directory, then you can do this:
public static void PackToZipWithoutInternalDir(string dir, string zipout, string comment = "")
{
if (Directory.Exists(dir) && !string.IsNullOrWhiteSpace(dir) && !string.IsNullOrWhiteSpace(zipout))
{
try
{
using var zip = ZipStorer.Create(zipout, comment); // true for stream
zip.EncodeUTF8 = true; // Text encoding
zip.ForceDeflating = true; // Force file compression
foreach (string listDir in Directory.EnumerateDirectories(dir, "*", SearchOption.TopDirectoryOnly))
{
// Add folders with files to the archive
zip.AddDirectory(ZipStorer.Compression.Deflate, listDir, string.Empty);
}
foreach (string listFiles in Directory.EnumerateFiles(dir, "*.*", SearchOption.TopDirectoryOnly))
{
// Add residual files in the current directory to the archive.
zip.AddFile(ZipStorer.Compression.Deflate, listFiles, Path.GetFileName(listFiles));
}
}
catch (Exception ex) { Console.WriteLine(ex); }
}
}
Use:
namespace ZipStorerEx
{
using System;
using System.IO;
public static class Program
{
private static readonly string CurrDir = Environment.CurrentDirectory;
private static readonly string BeginDir = Path.Combine(CurrDir, "YouDir");
private static readonly string ZipOut = $"{BeginDir}.zip";
[STAThread]
public static void Main()
{
Console.Title = "ZipStorerEx";
PackToZipWithoutInternalDir(BeginDir, ZipOut, "It's Good");
Console.ReadKey();
}
}
}

Related

zip a group of files by their file name

I have a list of logfiles which have their creation-date in their filename. I'm trying to zip the files together by date. Obviously I'm doing something wrong. How can I achieve this? This program is going to be a scheduled executable that zip log files every day, hence the currentDate variable. It could be 1 or several logfiles so the program should not depend on a number of files to zip.
These are the names of the log files:
Log_2021-06-17_1.txt,
Log_2021-06-17_2.txt,
Log_2021-06-17_3.txt
string currentDate = DateTime.Today.ToString("yyyy-MM-dd");
var logDir = new DirectoryInfo(#"C:\Users\user\Desktop\LogFilesTEST\In\");
foreach (FileInfo logFile in logDir.GetFiles())
{
if (logFile.Name.Contains(currentDate))
{
string startPath = #"C:\Users\user\Desktop\LogFilesTEST\In\" + logFile.Name;
string zipPath = #"C:\Users\user\Desktop\LogFilesTEST\Archive\" + logFile.Name + ".zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
}
}
Edit: if someone has the same problem here is what i did in my Console application:
Program Class
class Program
{
static void Main(string[] args)
{
string currentDate = DateTime.Today.ToString("yyyy-MM-dd");
var logDir = new DirectoryInfo(#"C:\Users\user\Desktop\LogFilesTEST\In\");
List<FileInfo> LogFileList = new List<FileInfo>();
foreach (FileInfo logFile in logDir.GetFiles())
{
if (logFile.Name.Contains(currentDate))
{
LogFileList.Add(logFile);
}
}
Zipper.CreateZipFile(LogFileList, #"C:\Users\user\Desktop\LogFilesTEST\Archive\Logs_" + currentDate + ".zip");
foreach(var logfile in LogFileList)
{
logfile.Delete();
}
}
}
Zipper Class:
class Zipper
{
public static void CreateZipFile(List<FileInfo> files, string archiveName)
{
using (var stream = File.OpenWrite(archiveName))
using (ZipArchive archive = new ZipArchive(stream, System.IO.Compression.ZipArchiveMode.Create))
{
foreach (var item in files)
{
archive.CreateEntryFromFile(item.FullName, item.Name, CompressionLevel.Optimal);
}
}
}
}

Copy files from folder A to folder B

I want to copy some of the content of a folder (recursively). only files that contain a specific pattern.
Here there's a method that copies the entire content. no matter what files inside.
https://stackoverflow.com/a/3822913/7028967
Do you have any ideas on how can I copy a specific file inside some subfolder with a given pattern?
for example:
-- rootFolder
---- filename.txt
CopyContent(src, dest, *.txt)
Below sample code to copy all the files from the source directory to destination folder in exact same folder structure :
public static void Copy()
{
string sourceDir = #"C:\test\source\";
string destination = #"C:\test\destination\";
string[] textFiles = Directory.GetFiles(sourceDir, "*.txt", SearchOption.AllDirectories);
foreach (string textFile in textFiles)
{
string fileName = textFile.Substring(sourceDir.Length);
string directoryPath = Path.Combine(destination, Path.GetDirectoryName(fileName));
if (!Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);
File.Copy(textFile, Path.Combine(directoryPath, Path.GetFileName(textFile)), true);
}
}
Updated:
I used this solution. and it works for me!
private static void CopyContentFolder(string sourcePath, string targetPath, bool isFileOverridable, string pattern = "*.*")
{
string[] sourceFiles = Directory.GetFiles(sourcePath, pattern, SearchOption.AllDirectories);
foreach (string sourceFilePath in sourceFiles)
{
string destinationFilePath = string.Empty;
destinationFilePath = sourceFilePath.Replace(sourcePath, targetPath);
EvaluatePath(destinationFilePath);
File.Copy(sourceFilePath, destinationFilePath, isFileOverridable);
}
}
private static void EvaluatePath(string path)
{
try
{
string folder = Path.GetDirectoryName(path);
if (!Directory.Exists(folder))
{
DirectoryInfo di = Directory.CreateDirectory(folder);
}
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
}

How to save .zip to a folder using ZipArchive?

I did the following, but i don't see the zip file in the directroy. C#
public static void AddToZip(string fileToAdd, string directory)
{
string entryName = fileToAdd.Replace(directory, string.Empty);
string archiveName = entryName.Replace(Path.GetExtension(entryName), ".zip");
using (ZipArchive za = ZipFile.Open(archiveName, ZipArchiveMode.Create))
{
za.CreateEntryFromFile(fileToAdd, entryName, CompressionLevel.Optimal);
}
}
and this is the link i followed.
http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive(v=vs.110).aspx
Finally got it working after some trial and error.
public static void AddToZip(string fileToAdd, string directory)
{
string entryName = fileToAdd.Replace(directory, string.Empty);//name of the file inside zip archive
string tempDir = Path.Combine(directory, Path.GetFileNameWithoutExtension(entryName));
if (Directory.Exists(tempDir)) DeleteDirector(tempDir);
else Directory.CreateDirectory(tempDir);
System.IO.File.Move(fileToAdd, Path.Combine(tempDir, entryName));//as the CreateFromDirectoy add all the file from the directory provided, we are moving our file to temp dir.
string archiveName = entryName.Replace(Path.GetExtension(entryName), ".zip"); //name of the zip file.
ZipFile.CreateFromDirectory(tempDir, Path.Combine(directory, archiveName));
DeleteDirector(tempDir);
}
private static void DeleteDirector(string deletedir)
{
foreach (string file in Directory.GetFiles(deletedir))
{
System.IO.File.Delete(file);
}
Directory.Delete(deletedir);
}
I know this is not the best solution. so, you are welcome to modify/improve it.

Forcefully Replacing Existing Files during Extracting File using System.IO.Compression?

I am using the following code to extract all files in a folder
using (ZipArchive archive = new ZipArchive(zipStream))
{
archive.ExtractToDirectory(location);
}
But if one file exist then it throws an exception. Is there is any way to tell the Compression API to replace the existing files.
I found one way is to get all the file names first then check whether file exist and delete it. But this is somehow very costly for me.
I have created an extension. any comment to it improve will be appreciated,
public static class ZipArchiveExtensions
{
public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
{
if (!overwrite)
{
archive.ExtractToDirectory(destinationDirectoryName);
return;
}
DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
string destinationDirectoryFullPath = di.FullName;
foreach (ZipArchiveEntry file in archive.Entries)
{
string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
{
throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
}
if (file.Name == "")
{// Assuming Empty for Directory
Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
continue;
}
file.ExtractToFile(completeFileName, true);
}
}
}
This code will not throw exception when the folder is not exist, instead of that it will create the folder.
public static class ZipArchiveExtensions
{
public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
{
if (!overwrite)
{
archive.ExtractToDirectory(destinationDirectoryName);
return;
}
foreach (ZipArchiveEntry file in archive.Entries)
{
string completeFileName = Path.Combine(destinationDirectoryName, file.FullName);
string directory = Path.GetDirectoryName(completeFileName);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
if (file.Name != "")
file.ExtractToFile(completeFileName, true);
}
}
}
Take a look at this: Creating zip files easily in .NET 4.5. Your problem seems to be adressed. Alternatively, you can also check DotNetZip.
As I'm a total Linq fan, the Linq-ish way just for reference:
using (var strm = File.OpenRead(zipPath))
using (ZipArchive a = new ZipArchive(strm))
{
a.Entries.Where(o => o.Name == string.Empty && !Directory.Exists(Path.Combine(basePath, o.FullName))).ToList().ForEach(o => Directory.CreateDirectory(Path.Combine(basePath, o.FullName)));
a.Entries.Where(o => o.Name != string.Empty).ToList().ForEach(e => e.ExtractToFile(Path.Combine(basePath, e.FullName), true));
}
You can extract files to some temp directory and than copy files with the
"File.Copy" with the ovveride option true to your destination directory
I know that it's not a perfect solution , but this way you do not need to
check if file exist
Here is a method that takes a path to the zip file.
Based on the accepted answer.
public void ExtractZipFileToDirectory(string sourceZipFilePath, string destinationDirectoryName, bool overwrite)
{
using (var archive = ZipFile.Open(sourceZipFilePath, ZipArchiveMode.Read))
{
if (!overwrite)
{
archive.ExtractToDirectory(destinationDirectoryName);
return;
}
DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
string destinationDirectoryFullPath = di.FullName;
foreach (ZipArchiveEntry file in archive.Entries)
{
string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
{
throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
}
if (file.Name == "")
{// Assuming Empty for Directory
Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
continue;
}
file.ExtractToFile(completeFileName, true);
}
}
}
Since .NET Standard 2.1, it's as easy as setting overwriteFiles to true in:
ZipFile.ExtractToDirectory(string sourceFile, string destDir, Encoding entryNameEncoding, bool overwriteFiles)
Example:
ZipFile.ExtractToDirectory("c:\\file.zip","c:\\destination_folder", Encoding.UTF8, true);
Hi I'm using DotNetZip download from nugget.
I just simply use this code.
This will auto replace the files in the directory if exists.
"OverwriteSilently" !
using (ZipFile archive = new ZipFile(#"" + System.Environment.CurrentDirectory + "\\thezipfile.zip"))
{
archive.ExtractAll(#"" + System.Environment.CurrentDirectory, ExtractExistingFileAction.OverwriteSilently);
}
This is useful when you have zip file path
public static class ZipArchiveHelper
{
public static void ExtractToDirectory(string archiveFileName, string destinationDirectoryName, bool overwrite)
{
if (!overwrite)
{
ZipFile.ExtractToDirectory(archiveFileName, destinationDirectoryName);
}
else
{
using (var archive = ZipFile.OpenRead(archiveFileName))
{
foreach (var file in archive.Entries)
{
var completeFileName = Path.Combine(destinationDirectoryName, file.FullName);
var directory = Path.GetDirectoryName(completeFileName);
if (!Directory.Exists(directory) && !string.IsNullOrEmpty(directory))
Directory.CreateDirectory(directory);
if (file.Name != "")
file.ExtractToFile(completeFileName, true);
}
}
}
}
}
A little remade method from the answer to create all the folders
public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
{
if (!overwrite)
{
archive.ExtractToDirectory(destinationDirectoryName);
return;
}
foreach (ZipArchiveEntry file in archive.Entries)
{
string completeFileName = Path.Combine(destinationDirectoryName, file.FullName);
if (file.Name == "")
{// Assuming Empty for Directory
Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
continue;
}
// create dirs
var dirToCreate = destinationDirectoryName;
for (var i = 0; i < file.FullName.Split('/').Length - 1; i++)
{
var s = file.FullName.Split('/')[i];
dirToCreate = Path.Combine(dirToCreate, s);
if (!Directory.Exists(dirToCreate))
Directory.CreateDirectory(dirToCreate);
}
file.ExtractToFile(completeFileName, true);
}
}
Looks like the only way to dodge including that blob of code is to simply delete the files before extracting the archive with del:
del (location + "\*")
using (ZipArchive archive = new ZipArchive(zipStream))
{
archive.ExtractToDirectory(location);
}
It's not exactly what the OP wanted, but it is compact.

Searching for files with a .dcm extension in a particular drive c#

I'm trying to search for files of extension .dcm, by entering either the filename or the content of the file. I am able to search in a directory, but when i try searching in a drive, I get an error that says missing directory or an assemble reference.
string startFolder = #"C:\";
// Take a snapshot of the file system.
System.IO.DriveInfo dir = new System.IO.DriveInfo(startFolder);
// This method assumes that the application has discovery permissions
// for all folders under the specified path.
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.
SearchOption.
AllDirectories);
Try to search in drive like in folder it works:
string startFolder = #"c:\";
DirectoryInfo directoryInfo = new DirectoryInfo(startFolder);
IEnumerable<System.IO.FileInfo> fileList = directoryInfo.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
Use this recursive function to avoid exceptions:
public static IEnumerable<string> GetFiles(string path, string pattern)
{
IEnumerable<string> result;
try
{
result = Directory.GetFiles(path, pattern);
}
catch (UnauthorizedAccessException)
{
result = new string[0];
}
IEnumerable<string> subDirectories;
try
{
subDirectories = Directory.EnumerateDirectories(path);
}
catch (UnauthorizedAccessException)
{
subDirectories = new string[0];
}
foreach (string subDirectory in subDirectories)
{
IEnumerable<string> subFiles = GetFiles(subDirectory, pattern);
result = result.Concat(subFiles); //This is LINQ concatenation
}
return result;
}
static void Main(string[] args)
{
string startFolder = #"c:\";
foreach (string fileName in GetFiles(startFolder, "*.chm"))
{
Console.WriteLine(fileName);
}
You can filter out immediatelly all the files with *.dcm extension
string startFolder = #"c:\";
DirectoryInfo directoryInfo = new DirectoryInfo(startFolder);
IEnumerable<System.IO.FileInfo> fileList = directoryInfo.GetFiles("*.dcm", System.IO.SearchOption.AllDirectories);
After that use foreach loop to find either a file with the desired name or open the file for reading and search for the value inside the file.
Are *.dcm text-based files or binary files? If they are text-based files you could use regulare expression to figure out whether the search string exists or not.
EDIT: Here is the sample recursive function that does the job. It is a console application so adapt it for your needs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace FileSearcher
{
class Program
{
static string searchValue = "Windows";
static List<FileInfo> files = new List<FileInfo>();
static void Main(string[] args)
{
string startFolder = #"C:\";
DirectoryInfo diParent = new DirectoryInfo(startFolder);
FindSearchValue(diParent);
foreach (var file in files)
{
Console.WriteLine("{0}", file.FullName);
}
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
/// <summary>
/// Recursive function that searches for a file that matches the criteria.
/// If the file is not found, the current file is opened and it's contents is
/// scanned for search value.
/// </summary>
/// <param name="diParent">Current parent folder being searched.</param>
private static void FindSearchValue(DirectoryInfo diParent)
{
FileInfo[] foundFiles = diParent.GetFiles("*.doc");
foreach (var file in foundFiles)
{
Console.WriteLine(file.FullName);
if (file.FullName.Contains(searchValue)) // There is a search string in a file name
{
files.Add(file);
}
else
{
string fileContents = File.ReadAllText(file.FullName);
if (fileContents.Contains(searchValue)) // Here you can use Regex.IsMatch(fileContents, searchValue)
{
files.Add(file);
}
}
}
foreach (var diChild in diParent.GetDirectories())
{
try
{
FindSearchValue(diChild);
}
catch (Exception exc)
{
Console.WriteLine("ERROR: {0}", exc.Message);
}
}
}
}
}
This function uses try-catch block to intercept exceptions that might occur. For example, file not found or access denied. Hope this helps!

Categories