I am able to move the directory in azure using ShareDirectoryClient successfully.
using System;
using System.Threading.Tasks;
using Azure.Storage.Files.Shares;
namespace SO69798149
{
class Program
{
const string MyconnectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key";
const string MyshareName = "share-name";
const string SourceDirectoryName = "source-directory-name";
private const string RenamedDirectoryName = "new-directory-name";
static async Task Main(string[] args)
{
ShareClient myshare = new ShareClient(MyconnectionString, MyshareName);
ShareDirectoryClient sourceDirectoryClient = myshare.GetDirectoryClient(SourceDirectoryName);
ShareDirectoryClient targetDirectoryClient = myshare.GetDirectoryClient(RenamedDirectoryName);
await RenameDirectory(sourceDirectoryClient, targetDirectoryClient);
Console.WriteLine("Directory renamed.");
}
static async Task RenameDirectory(ShareDirectoryClient sourceDirectoryClient,
ShareDirectoryClient targetDirectoryClient)
{
//Create target directory
await targetDirectoryClient.CreateIfNotExistsAsync();
//List files and folders from the source directory
var result = sourceDirectoryClient.GetFilesAndDirectoriesAsync();
await foreach (var items in result.AsPages())
{
foreach (var item in items.Values)
{
if (item.IsDirectory)
{
//If item is directory, then get the child items in that directory recursively.
await RenameDirectory(sourceDirectoryClient.GetSubdirectoryClient(item.Name),
targetDirectoryClient.GetSubdirectoryClient(item.Name));
}
else
{
//If item is file, then copy the file and then delete it.
var sourceFileClient = sourceDirectoryClient.GetFileClient(item.Name);
var targetFileClient = targetDirectoryClient.GetFileClient(item.Name);
await targetFileClient.StartCopyAsync(sourceFileClient.Uri);
await sourceFileClient.DeleteIfExistsAsync();
}
}
}
//Delete source directory.
await sourceDirectoryClient.DeleteIfExistsAsync();
}
}
}
I am moving the directory in azure using ShareDirectoryClient. Here How can we zip the folder after moving it.
My Approach:
using System.IO.Compression;
ZipFile.CreateFromDirectory(sourceDirectoryClient.Path, targetDirectoryClient.Path);
Error: Could not find a part of the path '/app/NewFolder/test.zip'.
Please assist me in resolving the issue
Note: We can also use the below library https://github.com/icsharpcode/SharpZipLib
Zipping a directory using C# and SharpZipLib
void fastcompressDirectory(string DirectoryPath, string OutputFilePath, int CompressionLevel = 9)
{
ICSharpCode.SharpZipLib.Zip.FastZip z = new ICSharpCode.SharpZipLib.Zip.FastZip();
z.CreateEmptyDirectories = true;
z.CreateZip(OutputFilePath, DirectoryPath, true, "");
if (File.Exists(OutputFilePath))
Console.WriteLine("D0ne");
else
Console.WriteLine("Failed");
}
The above code will zip all the folder contents to a new zip file. How can we achieve the same in azure sharedirectoryclient directory.
My understanding is that you want to compress a directory from Azure File Share and store the result zip archive on the same Azure File Share.
The problem you have with
ZipFile.CreateFromDirectoryis related to paths and where you execute the code. If you substitute those paths you will have something like:Those paths are treated as local to your application (if your app is in
C:\MyAppthen they will resolve toC:\MyApp\app\NewFolder).ZipFiledoes not use Azure File Share and this is the main problem.You can try to use Azure Data Factory to zip folders or maybe Josefs Ottosson tutorial will be helpful if you want to implement this in your code.
Or maybe you can use this approach if you attach the share to the machine on which your app is running then you would operate on files on this machine.