0

I want to upload a set of files into azure-blob-storage.I have to pass the files in zip format while calling upload method inside the controller(Am using postman to make this call).Though am getting the zipped data inside the controller, am unable to extract files in this zipped data.

Below Image shows how I call the api controller and pass the zip file using postman.

enter image description here

 [HttpPost]
    public HttpResponseMessage PostFiles()
    {

        var zipfile = Request.Form.Files;
        ..... 

if I get streams of individual files from the zipped file, I would be able to upload the value to azure blob and get the respective file Id as my desired output.

Please provide a solution for whether any third party dll will be required to fetch the individual files details inside the zip file or this issue could be resolved without any third party tool.

Swaroop
  • 501
  • 5
  • 18
  • You can unzip the file into a temporary location to access the individual files. https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile.extracttodirectory?view=netframework-4.8 – Reinstate Monica Cellio May 17 '19 at 13:52
  • 2
    Possible duplicate of [C# IFormFile as ZipFile](https://stackoverflow.com/questions/46220025/c-sharp-iformfile-as-zipfile) – William Xifaras May 17 '19 at 13:53
  • Possible duplicate of [How do I ZIP a file in C#, using no 3rd-party APIs?](https://stackoverflow.com/questions/940582/how-do-i-zip-a-file-in-c-using-no-3rd-party-apis) – Avi Meltser May 17 '19 at 13:54

2 Answers2

1

I think you can just do the following:

string ZipPath = Path.GetFullPath("myfile.zip"));
//ZipPath is "C:\temp\Demo\myfile.zip"

ZipArchive zipFile = ZipFile.Open(ZipPath, ZipArchiveMode.Read);
ZipArchiveEntry fileFromArchive = zipFile.GetEntry(FileName);

And use this namespace:

using System.IO.Compression;
Ivan B
  • 84
  • 8
  • ZipArchiveMode.Update is for editing, ZipArchiveMode.Read is for extraction otherwise the right answer – MikeT May 17 '19 at 14:04
  • Right. Thanks for noting! – Ivan B May 17 '19 at 14:13
  • Cannot get the zip file path - ZipPath as mentioned in your code. – Swaroop May 20 '19 at 06:00
  • Oh, sorry. ZipPath in my code is a string parameter (i.e. I declared and initialized it myself). I use `Directiry.GetCurrentDirectory` method together with `Path.GetFullPath` method to get the path of my zip file. So, the ZipPath string looks like: C:\temp\Demo\myfile.zip. Here are the references for two methods I'm using: [GetFullPath](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfullpath?view=netframework-4.8) , [GetCurrentDirectory](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getcurrentdirectory?view=netframework-4.8) – Ivan B May 20 '19 at 12:53
0

How about using SharpZipLib? Looks easy enough to use and there's a good amount of documentation and examples.

hmiedema9
  • 948
  • 4
  • 17