DotNetZip how to delete a file after extracting

不羁的心 提交于 2021-01-29 06:45:17

问题


I found out a problem in my project. I'm currently using a DotNetZip library to extract zip - files. Additionally, I need to delete a zip file after extracting it's content. However, I'm unable to do it (both manually and using File.Delete(...)), cause the folder is open in my programm. So, I think this problem is caused by file extraction process The "suspicious" code part:

string localFileName = @"...path....";

            string remoteDir = "/SFTPfolder/";

            using (var sftp = new SftpClient(SFTPHost, SFTPUsername, SFTPPassword))
            {
                sftp.Connect();

                var files = sftp.ListDirectory(remoteDir);



                foreach (var x in files)
                {
                    if (!x.Name.StartsWith("."))
                    {
                        Console.WriteLine(x.Name.ToString());

                        string rfn = x.Name;

                        using (Stream st1 = File.OpenWrite(localFileName + rfn))
                        {

                            sftp.DownloadFile(remoteDir + rfn, st1);
                        }
                    }
                }

            }

            UnzipFiles();
            File.Delete(@"BLA\BLA1\BLA2\ZIPFILE.zip");

Where UnzipFiles is:

public static void UnzipFiles()
    {

        string zipFilePath = @"BLA\BLA1\BLA2\ZIPFILE.zip";
        string extractedFilepath = @"BLA\BLA1\BLA2";

        ZipFile zipfile = ZipFile.Read(zipFilePath);
        if (!Directory.Exists(extractedFilepath))
        {
            Directory.CreateDirectory(extractedFilepath);
        }

        foreach (ZipEntry e in zipfile)
        {
            e.Extract(extractedFilepath, ExtractExistingFileAction.OverwriteSilently);
        }

    }

So, my question is: How to make this file "free"? (In order to delete it)

来源:https://stackoverflow.com/questions/54931692/dotnetzip-how-to-delete-a-file-after-extracting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!