I have an Azure Function (HttpTrigger) which creates a tar.gz file in my Azure Storage.
When I trigger it one time, the file is succesfully created. but if I hit it again I have an error:
[Info] The process cannot access the file 'D:\local\Temp\myDir\tarArchive.tar.gz' because it is being used by another process.
I have to restart the function in the Azure Portal to be able to create another file.
here is my code:
FileStream fs = new FileStream(myDir+"/firstFile", FileMode.Create);
fs.Write(bytesToCompress, 0, bytesToCompress.Length);
fs.Dispose();
FileStream sfs = new FileStream(myDir + "/secondfile", FileMode.Create);
sfs.Dispose();
DirectoryInfo DirectoryOfFilesToBeTarred = new DirectoryInfo(myDir);
FileInfo[] filesInDirectory = DirectoryOfFilesToBeTarred.GetFiles();
string tarArchiveName = myDir + "/tarArchive.tar.gz";
using (Stream targetStream = new GZipOutputStream(File.Create(tarArchiveName)))
{
using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(targetStream, TarBuffer.DefaultBlockFactor))
{
foreach(FileInfo fileToBeTarred in filesInDirectory)
{
log.Info(fileToBeTarred.FullName);
TarEntry entry = TarEntry.CreateEntryFromFile(fileToBeTarred.FullName);
tarArchive.WriteEntry(entry, true); // Error thrown here
}
}
}
I thought fileToBeTarred was still in use when the function is called again (am I wrong?) but I've tried to create a stream from this FileInfo in order to Dispose() it, but didn't fix my problem. I've also tried to Delete() it, without any effect.
Is someone seeing what I don't see ?
Thanks for your help
UPDATE
Here is the corected code given by Wim Coenen
using (Stream fileStream = File.Create(tarArchiveName))
using (Stream targetStream = new GZipOutputStream(fileStream))
using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(targetStream, TarBuffer.DefaultBlockFactor))
{
foreach (FileInfo fileToBeTarred in filesInDirectory)
{
log.Info(fileToBeTarred.FullName);
TarEntry entry = TarEntry.CreateEntryFromFile(fileToBeTarred.FullName);
tarArchive.WriteEntry(entry, true); // Error thrown here
}
}
And the log of the error (e = Exception object) e.message =>
2018-08-01T11:59:46.887 [Info] The process cannot access the file 'D:\local\Temp\myDir\tarArchive.tar.gz' because it is being used by another process.
e.ToString() =>
2018-08-01T11:59:47.152 [Info] System.IO.IOException: The process cannot access the file 'D:\local\Temp\myDir\tarArchive.tar.gz' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at ICSharpCode.SharpZipLib.Tar.TarArchive.WriteEntryCore(TarEntry sourceEntry, Boolean recurse)
at ICSharpCode.SharpZipLib.Tar.TarArchive.WriteEntry(TarEntry sourceEntry, Boolean recurse)
at UploadFileFromBCText.Function1.d__4.MoveNext()