Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Al Burns 49 posts 149 karma points
    Feb 26, 2018 @ 09:28
    Al Burns
    0

    Issue unzipping .zip media item

    Hi.

    I don't think this is strictly an issue with Umbraco but I would be grateful for any assistance for this odd issue.

    I have created a media type to store a zip file in the library and have added some code to the Media Service Saved event to unzip the file into a directory on the web server. I use the following code example to achieve this:

    public static void MediaService_Saved(IMediaService sender, SaveEventArgs<IMedia> e)
    {
            if (e.SavedEntities.Any(x => x.ContentType.Alias.Equals("MyItem")))
            {
                var items = e.SavedEntities
                    .Where(x => x.ContentType.Alias.Equals("MyItem"))
                    .ToList();
    
                items.ForEach(UnpackObject);
             }
    }
    
    private static void UnpackObject(IMedia item)
    {
            var path = item.GetValue<string>("umbracoFile");
            var absolutePath = HttpContext.Current.Server.MapPath(path);
            var fileName = Path.GetFileNameWithoutExtension(absolutePath);
    
            var targetDir = AppDomain.CurrentDomain.BaseDirectory + @"Content\";
    
            ZipFile.ExtractToDirectory(absolutePath, targetDir);
    }
    

    My issue is that when I run this locally the ExtractToDirectory method creates a folder with the same name as the .zip file under the target directory and unzips the file into that, which is the required behaviour, but when I deploy the code to a web server it doesn't create the sub-folder and extracts the file contents directly into the target directory, e.g.:

    Locally

    Zip file: /media/1001/my-test-zip.zip

    Target Directory: /Content/

    Result: /Content/my-test-zip/zip contents

    Web Server

    Zip file: /media/1001/my-test-zip.zip

    Target Directory: /Content/

    Result: /Content/zip contents

    Has anyone else encountered this issue and confirm what you had to do to resolve it. Currently I am using the file name and appending this to the target directory when using the extract method but this is not ideal when trying to test locally.

    Many thanks.

  • Nigel Wilson 945 posts 2077 karma points
    Feb 26, 2018 @ 18:34
    Nigel Wilson
    0

    Hi Al

    Probably not a solution but...

    Would it be worth checking if the sub directory exists, and if not, create it prior to extracting ?

    Just thinking that permissions of app pool on live environment might be the issue and so explicitly creating the sub directory might help.

    Cheers

    Nigel

  • Al Burns 49 posts 149 karma points
    Feb 27, 2018 @ 11:05
    Al Burns
    0

    Hi Nigel, thanks for the reply.

    I have initially created the parent "Content" folder and this sits under the IIS account along with all the other site folders. This is the same both on my local development environment and on the UAT web server.

    The issue is with the creation of the sub-folder based on the name of the zip file; on the development environment this sub-folder is automatically created by the ZipFile's extract method and the contents extracted into this, but when on the UAT environment it doesn't do this so I have to manually add the sub-folder name to the target directory path when passing to the extract method. It then correctly extract the zip contents into this folder.

  • Nigel Wilson 945 posts 2077 karma points
    Feb 27, 2018 @ 16:08
    Nigel Wilson
    0

    Hi Al

    Sorry, I was meaning the zip folder name in my initial response.

    What about adding something along the lines of the following to provide for the different environments.

    bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));
    
    if(!exists)
        System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
    

    Appreciate that your local machine does it automatically, but for the sake of a couple of lines of code, you will know the directory issue is absolutely solved no matter the environment.

    Cheers, Nigel

  • Al Burns 49 posts 149 karma points
    Feb 27, 2018 @ 16:23
    Al Burns
    0

    Hi Nigel.

    That's a good suggestion, but just to be awkward the ZipFile extract method throws an exception if the directory you are unzipping to already exists!

  • Nigel Wilson 945 posts 2077 karma points
    Feb 27, 2018 @ 18:12
    Nigel Wilson
    0

    Hi Al

    Just an idea - comment out the unzip part of your code and try creating the sub directory using my prior suggestion. If this fails then that points (most likely) to server permissions.

    Cheers, Nigel

Please Sign in or register to post replies

Write your reply to:

Draft