Foufhs
Foufhs

Reputation: 53

Get the filename of a file that was created through ZipFile.ExtractToDirectory()

string zipPath = @"D:\books\"+fileinfo.DccFileName;
string extractPath = @"D:\books";
System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);

This is a simple piece of code that does exactly what i want it to do: Gets a zip file from d:\books and unzips it into the same directory. Is there any way i can read the filename of the newly created file (considering that there is only one file in the .zip archive). I would prefer a solution that does not involve reading changes in the directory since other files might be created in it at the same time of the unzip.

Upvotes: 1

Views: 2205

Answers (1)

Nkosi
Nkosi

Reputation: 247088

You can construct the path by inspecting the archive

var intentedPath = string.Empty;
//open archive
using (var archive = ZipFile.OpenRead(zipPath)) {
    //since there is only one entry grab the first
    var entry = archive.Entries.First();
    //the relative path of the entry in the zip archive
    var fileName = entry.FullName;
    //intended path once extracted would be
    intentedPath = Path.Combine(extractPath, fileName);
}

Upvotes: 3

Related Questions