Hi Rob,
1.@Trevor Lacey 's sample code was great , it solved the first problem of extracting zip file to flatten files with rename.
2.I have another problem of making implementation of
System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, extractionPath, true); instead of override to make rename.
i tried to first extract the zip file to temp extraction path.
Later to traverse recursively the files and folder.
In process folder function i have problem to find the path
of the gaped sub folder in order to create it .
Help will be appreciated.
string zipFilePath = zipPathForFolderOrPathForFile;
string extractionPath = SelectedItem.Path;
string guid = Guid.NewGuid().ToString();
string tempExtractionPath = Path.Combine(extractionPath, guid);
System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath,tempExtractionPath);// no override
ProcessDirectory(tempExtractionPath, guid);
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
private void ProcessDirectory(string sourceDirectory, string guid)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDirectory);
foreach (string fileName in fileEntries)
ProcessFile(fileName, guid);
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(sourceDirectory);
foreach (string subdirectory in subdirectoryEntries)
{
// I have problem with creating the new subfolder that not exists.
// var newSubDirectory = subdirectory.Substring(0, subdirectory.LastIndexOf(Path.DirectorySeparatorChar));
// if(!Directory.Exists(newSubDirectory))
// {
// Directory.CreateDirectory(newSubDirectory);
// }
ProcessDirectory(subdirectory, guid);
}
}
// Insert logic for processing found files here.
private void ProcessFile(string path, string guid)
{
var newFile = path.Substring(0, path.LastIndexOf(Path.DirectorySeparatorChar));
newFile = newFile.Replace(Path.Combine(Path.DirectorySeparatorChar.ToString(),guid), string.Empty);
newFile = Path.Combine(newFile,Path.GetFileName(path));
int count = 1;
while (File.Exists(newFile))
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(newFile);
string fileExtension = Path.GetExtension(newFile);
newFile = Path.Combine(Path.GetDirectoryName(newFile),$"{fileNameWithoutExtension}_Copy{count}{fileExtension}");
count++;
}
File.Create(newFile);
}