I'm using System.IO.Compression in order to compress a file into a .zip, below the source code:
using (FileStream zipToOpen = new FileStream(zipName, FileMode.CreateNew)){
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)){
ZipArchiveEntry readmeEntry = archive.CreateEntry(@"C:\Users\soc\myFold\someFile.xml");
}
}
This piece of code works well, but unfortunately in the .zip there's the entire sequence of folders (C: -> Users -> ... -> someFile.xml); can I obtain a final .zip with ONLY the file I need? I know that with other libraries this fact is possible (DotNetZip add files without creating folders), but I would like to know if it were possible do the same with the standard library.
You seem to be under the impression that the file will be added to the archive, which is not the case.
CreateEntry
merely creates an entry with the specified path and entry name, you still need to write the actual file.In fact, the code in your question is quite similar to the code in the documentation, so I assume you got it from there?
Anyway, to get only the file name you can use
Path.GetFileName
and then you can write the actual file content to the zip entry.The code above will create an archive with
foo.txt
in the root and with the content of the source file, without any additional directories.