16

I want to create a 7zip file containing files with the same names but in different folders using 7zip's @listfile feature. Although I have used 7zip CLI for a long time, I just cannot find the syntax to accomplish this.

My file tree looks like this (note that somefile1.html and somefile2.html occur twice each).

|   somefile2.html
+---dir1
|       somefile1.html
|       somefile2.html
|       
+---dir2
|       somefile3.html
|       somefile4.html
|       
\---dir3
        somefile1.html
        somefile5.html

Using a @listfile works fine, but I cannot figure out how to retain the directory tree in the resulting 7zip while doing that.

I have tried to the following syntax:

7z a -ir@files.txt my_compressed_file.7z

Then, given the explanations in 7zip's Windows CHM help file under syntax, I tried including the root folder, too (called 'files'):

7z a -ir@files.txt my_compressed_file.7z .\files

I get the same error in both cases:

Error
Duplicate filename:
somefile1.html
somefile2.html

Has anyone figured this one out and would care to shed some light on it?

I know how to compress files with the same names in different folders otherwise (when the folder structure is retained in the 7zip it s no problem). But this time the few files I need are spread all over the place...

jockster
  • 397
  • 1
  • 3
  • 10
  • This has been a HUGE bug in 7zip for many years. It's been reported many times to 7zip's authors... but they just don't seem to understand it. I avoid 7zip. – Gayle Mar 23 '13 at 04:37

5 Answers5

8

The following commands for windows, using the shell and 7za (the command-line version of 7zip) could probably be adapted for other platforms; but this solved the problem for me:

1) Make a list of all the files you want to archive, including full paths, with a command like the following; suppose your command prompt is in the C:\ directory and you want to list all *.xmp files in all sub-directories on the C: drive, for archiving in the next step:

DIR /B /S *.xmp > XMPsToArchiveList.txt

2) Use XMPsToArchiveList.txt as the list file for 7za, with the -spf switch, which instructs 7za to "use fully qualified file paths:"

7za a -spf -ir@XMPsToArchiveList.txt allCdriveXMPs.7z

If all goes well, you should receive the blissful message: "Everything is OK," and see a new and proper so named .7z archive.

In my test, the resulting .7z archive has folders and sub-folders reflecting the full paths to all .xmp files, with the root folder titled "C:".

NOTES:

A) On Windows, to adapt the list command to include everything in so many directories of a given name (in so many different places), you can use e.g.:

DIR /S /B *_aCertainFolderNameDuplicatedAllOverTheDrive* > foldersToArchive.txt

B) The -spf switch was added 2011-09-16; re: http://www.7-zip.org/history.txt

Also, I tried wildcards trying to produce a list like you describe, but didn't have any luck getting all files named e.g. somefile*.txt into a list (such as archiveTheseFiles.txt). Maybe I mis-typed? Seems simple. You'll need to mess around at the prompt to find what works for you.

Finally, I don't know whether the -spf switch is available for 7z (as opposed to 7za). 7za is available yonder, in the "extras" download: http://www.7-zip.org/download.html

Alex Hall
  • 956
  • 10
  • 18
  • Thanks for some really useful insight into the 7z command line. Marking your post as the answer. This addressed a related issue I had wrt zipping up modified files, with the directory structure intact, for backup purposes. The original problem was also solved - just confirmed that. Thanks! – jockster Dec 09 '16 at 07:10
  • You can use PowerShell to generate the list file and keep relative paths instead of absolute path: `ls *.xmp -Recurse | Resolve-Path -Relative | %{$_.Substring(2)} | ac XMPsToArchiveList.txt` – Gregor y Jun 21 '18 at 04:29
7

I ran into the same error (Duplicate filename) while using a list file. My list file looked like this:

D:\SomeDir\SomeFile.txt
D:\SomeOtherDir\SomeFile.txt

I resolved the problem by launching 7-zip from D:\ and editing my list file to use relative rather than absolute paths:

SomeDir\SomeFile.txt
SomeOtherDir\SomeFile.txt

The resulting archive preserved the original directory structure and looked exactly as I expected.

Note: I was using 7-Zip 4.65, and this solution will not work if the files are on different drives or have a different root.

The PowerShell command line I ran was:

& 'C:\Program Files\7-Zip\7z.exe' a -scsWIN logs.7z `@recentlogs.txt
bart
  • 1,100
  • 11
  • 14
  • Exactly what I needed to pack things with absolute paths, thanks. :) – Adambean Apr 30 '14 at 16:48
  • This works, just remember to trim off any leading path chars ('\'). – Derf Skren Dec 12 '14 at 05:23
  • When I want to store off a project I'm working on (for example, I'm in the middle of one right now, where I tried out a number of different tactics, I've settled on one, and I'm about to strip out all of the other code, but I want to archive it with the code-as-examples) - my project is called "Mirror" (in a "Mirror" folder in D:\prog\csharp): cd \prog\csharp 7za.exe a -t7z D:\prog\archives\csharp_Mirror_20151127.7z @D:\prog\bkp\Mirror\program_Mirror_backuplist.txt -mx=9 -ms=off (The "program_Mirror_backuplist.txt" being a list of the specific files I want to back up.) – Steve Greene Nov 29 '15 at 01:47
3

My real directory structure was:

+---Release
|   |   Lib.dll
|   \--- x64
|           Lib.dll
|       
+---Definition_files
|       data_files
|       
+---x64
|       Lib.dll // Copy of the previous file (only to preserve "x64" directory")
\---Documentation
        documentation_files....

I used the list file:

 .\Release\Lib.dll // used ".\" to remove "Release" directory
 Definition_files\data_files
 Documentation\documentation_files
 x64\Lib.dll

Commandline:

7z.exe a -mx=9 -mpass=5 -r .\Packgakes\release.zip .@Lib.lst

This caused a duplicity error. Without "-r" there is no error.

Try to check command line for recursive scan and list of files if there really are not two files with the same destination path (using "." in file path removes path in output).

Julo
  • 1,102
  • 1
  • 11
  • 19
2

Since 7z is buggy, if you insist on using 7z to compress, try using tar to create a .tar archive first then make a .tar.7z archive.

tarinstead
  • 21
  • 1
1

Since 7-zip 9.38 beta you can select path mode: relative, full, absolute.

denfromufa
  • 5,610
  • 13
  • 81
  • 138