1

I have a folder with subfolders and files. I create a .zip file via powershell's Compress-Archive.

Problem is that the file structure is messed up when opened in Linux.

Bad is the bad.zip archive made in Powershell with Compress-Archive, and Good is good.zip made with Windows' WinRAR.

This is how it looks on Linux: (Left is bad, right is good)

picture 1

File contents seen in Linux with cat: (left is bad, right is good):

picture 2

How to fix this problem??

...

I tried replacing all "\" in bad.zip with "/" in nano and it worked.

I tried automating that with this script, but unsuccessfully:

#!/bin/bash

contents=$(cat $1)
echo "${contents//\\//}"

I run the script:

./FixZip.sh bad.zip > new.zip

The backslashes have been replaced, as intended, but new.zip cannot be opened. The reason is probably the encoding is different...

This is the encoding for the zip files:

terminal:# file -i bad.zip 
bad.zip: application/zip; charset=binary
terminal:# encguess bad.zip 
bad.zip UTF-32LE

terminal:# file -i new.zip 
new.zip: application/octet-stream; charset=binary
terminal:# encguess new.zip 
new.zip unknown

terminal:# cat new.zip
P5�WMCw:a_folder/test2.txt+��IP7�WM���a_folder/test3.txt�(*P4�WM��6a_folder/subf/test1.txt�H���P5�WMCw:a_folder/test2.txtP7�WM���7a_folder/test3.txt4�WM��6ma_folder/subf/test1.txtPKũ
  • 1
    Are you the only person on this forum?... Anyways.. I cannot use 7zip on windows, only powershell's Compress-Archive. I guess I can always recreate the file structure with a script.... – MyWays Oct 23 '18 at 21:00
  • 1
    No offense intended. It's just that he always helps me.. – MyWays Oct 23 '18 at 21:05
  • 1
    Several things wrong: (1) you can't edit a zip file without corrupting it, which will normally be detected by its check-sum; (2) $(cat $1) will replace any sequence of white-space characters by a single space; (3) back-slashes will occur in the compressed data, as well as in the file index, so replacing them will further corrupt the file. It's not obvious why the PK magic number should have been replaced, but I would guess return, back-space or escape characters in the binary would account for it. As @KamilMaciorowski recommends, xxd will clarify what's going on. – AFH Oct 23 '18 at 21:10
  • 2
    "I cannot use 7zip on windows, only powershell's Compress-Archive." - really? You've already said you can use WinRAR. 7-Zip is available as a Portable App, which doesn't need installation. – AFH Oct 23 '18 at 21:15
  • 3
    The version on the PortableApps web-site is completely portable. It can be launched either through the PA interface or directly from the PortableApps\ directory, which contains 32- and 64-bit versions of both the GUI and the 7z.exe command-line version, which I imagine will need one or more of the .dll files. – AFH Oct 23 '18 at 22:00
0

There are a number of things wrong with what you are trying to do - most significantly:-

  1. You can't edit a zip file without corrupting it: this will normally be detected by its check-sum, and a corrupt file will be reported.
  2. echo $contents (or other expansion variants) will replace any sequence of white-space characters by a single space.
  3. Back-slashes will occur in the compressed data, as well as in the file index, so replacing them will further corrupt the file.

The most satisfactory answer is to use a cross-platform archiver, such as 7-Zip, which will save an archive with generic directory separators and will expand in the native format for the platform where it's called.

In Windows I use the Portable Apps version, which can be run through the PortableApps Suite interface, or direct from its directory in isolation; the portable version contains both 32- and 64-bit builds and includes 7z.exe, the command-line interface (which may use a DLL from the same directory).

It's not obvious why in your logs the PK magic number should have been replaced, but I would guess that return, back-space or escape characters in the binary could account for it. As Kamil Maciorowski recommends in his comment, xxd will clarify what has been going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.