Solved: Ubuntu unzip with backslashes in file name.



This happened last week when I tried to extract a zip archive I received from a friend. When I extracted it in my Ubuntu Desktop, the extracted files did not have the same folder structure as the original source. I noticed the extracted files had the path as the filename. The different levels of the path were separated by backslashes. This is something I have never experienced before, and I was a bit confused at first because I wasn't aware of the original folder structure. Later only I realized the paths separated with backslashes should be in a folder hierarchy and not in the file name. So the paths shown in the file name with backslashes should be created when you extract the zip, but it didn't. So my research started to find the solution and after going through a few websites, questions and answers sites I realized that the issue was with the zip file, and not in my extracting software I used (zip).

My friend who sent me the zip archive was using Windows 8 and I was using Ubuntu 14.04. But I never had issues extracting zip archives created inside Windows, in Ubuntu before this. Then I heard from the friend that it was generated using an online tool. That could be the issue.

So the solution to this is, you have to extract the zip archive as you would do normally. Then go through each file name and see what paths and folders the file names are having and create the folders manually, then rename the file.

I know this is not the answer you expected in this blog post :) But unfortunately there was no proper easy one click solutions to this (I didn't find any). I found some Python scripts in some websites. But they all had issues. Some were not really working, and some were not giving the desired output. So I had to rely on my own skills and write a little script by myself to solve this. I'm a PHP developer, so it's the most comfortable scripting language I see around. You can write many useful automation scripts using PHP if you want :)

Here's the script I wrote. This is really easy. I'll tell you how to run this.

First, you need to have PHP installed in your Ubuntu desktop. I'm not going to teach you how to install PHP here because there are many good articles already written for that purpose. So once you have installed php, right click on your zip archive and extract it into any place you like.

Once you extracted it, go inside the folder. Now you should see all the extracted files in the same folder, with long file names having backslashes in their names. Don't worry.

Now right click inside that folder and create a new file. Name it as anything you like, but make sure it's a php file. That means it should end with a ".php" extension. I would name it as "create_folders.php".

Open the file you created with a text editor, or any code editor, IDE if you have. Then paste the below php script in the file and save it.

<?php

$files = glob('*');

foreach ($files as $file) {
    $new_file = str_replace("\\", "/", $file);

    if (!is_dir(dirname($new_file))) {
        mkdir(dirname($new_file), 0777, true);
    }

    rename($file, $new_file);
}

?>

Then open your terminal by pressing "CTRL + ALT + T", and CD into the folder where you extracted the zip archive, and run the php script by issuing the command;

php create_folder.php

Here's an example if you find it difficult to understand.

Let's assume I extracted the zip archive into "myfolder", and created the new file with php script as I explained above. Now if you list the "myfolder", it should be something similar to below list.

css\styles.css
css\common.css
images\home\bg.png
images\logo.png
index.html
create_folder.php <---- This is the new file we created.

Now open your terminal. CD into this folder..

nimeshka@PC:~$CD myfolder
nimeshka@PC:~/myfolder$ php create_folder.php

It will take a few seconds if your folder is large. Then once it's finished, go back to your folder and see the files are nicely arranged and new folders are created :)

So that's it guys. Hope this is helpful. Please let me know what you think about this by leaving a comment. Don't forget to share this article in Facebook, Twitter, G+ if you think it will help someone else :)

1 comments :

  1. Or you can unzip your file into a FAT-formatted drive or disk image: unzip will map backslashes to '/' in that case.

    ReplyDelete