7-zip commandline
Asked Answered
L

8

11

I'm creating a backup utility that zips all the files listed in a text file. I am using MS ACCESS as my front-end and 7-Zip for compression. These are the commands I am using:

7zG.exe a c:\Backup\backup.zip
@c:\temp\tmpFileList.txt

All the files are compressed without their path. Is there a way to include the path for each file in the zip file? Then when backup.zip is extracted the files would be restored to their original folder structure.

Thanks

Langlauf answered 2/9, 2009 at 8:5 Comment(2)
And if you manage to incorporate paths in the zip file, use relative paths or you won't be able to restore them anywhere else but the orignal location.Redware
"How can we make our tool as obtuse to use in the command line as possible...? Hmm... Oh I know! Like this!"Vlaminck
A
6

In this 7-zip forum thread, in which many people express their desire for this feature, 7-zip's developer Igor points to the FAQ question titled "How can I store full path of file in archive?" to achieve a similar outcome.

In short:

  • separate files by volume (one list for files on C:\, one for D:\, etc)
  • then for each volume's list of files,
    1. chdir to the root directory of the appropriate volume (eg, cd /d C:\)
    2. create a file listing with paths relative to the volume's root directory (eg, C:\Foo\Bar becomes Foo\Bar)
    3. perform 7z a archive.7z @filelist as before with this new file list
    4. when extracting with full paths, make sure to chdir to the appropriate volume's root directory first
Afterheat answered 29/4, 2012 at 7:4 Comment(0)
R
4

The command-line program for 7-Zip is 7z or 7za. Here's a helpful post on the options available. The -r (recurse) option stores paths.

Reconciliatory answered 2/9, 2009 at 8:10 Comment(1)
Thanks Vinay for the response.. But i cannot find 7za.exe in my 7-zip directory, also 7z.exe has 0 bytes. I used 7zG.exe because it displays a GUI progress bar rather than a DOS screen. The only problem is that its doesn't include the path where the file is locatedLanglauf
T
1

I've not looked into this but shooting from the hip I'd say that they dropped command line support in the portable. The reason people don't do much command line stuff in portable applications is that the OS (windows in your case) requires that executables be added to the %path% inclusion list.

If that requirement is not met using command line utilities is rather tedious.

7z -a .

would be

d:\portable\z7\z7 -a c:\to\archive\folder*.*

Typing that out for everything is why GUI's make sense with things like portable apps it (the app) can remember it's own location and handle that stuff for you and if you can't run it you know it's not attached.

If you really want the portable app to contain that though you can always install the full version and pull the required 7z.exe out and put it into the portable folder making sure it's in with the required dll's.

You'll have to set your path when you hit the shell after making sure it's attached.

http://www.redfernplace.com/software-projects/patheditor/ -- a good path editor (down) usefull if you have lots of path information 20+ get's hard to read.

http://www.softpedia.com/get/System/System-Miscellaneous/Path-Editor.shtml -- alternet source for path editor

It's not advisable to modify your system path for temproary "portable" drives though manualy do that by:

set path=%path%;"d:\portable\z7\";

when you run dos cmd.exe or http://sourceforge.net/p/conemu/home/Home/

The other answers address other problems better I'm not going to try..

http://www.codejacked.com/zip-up-files-from-the-command-line/ -- good reference for command line usage of z7 and z7a.

PS: sorry for the necro but I figured it needed a more direct answer to why (even if it's just speculative).

Throttle answered 28/5, 2013 at 4:33 Comment(0)
L
1

try this one. it worked for me. 7z.exe a d:\newFileName.7z "d:\ExistingFile.txt"

open cmd and if you have installed 7zip app then try this. on command prompt it will like c:\programs and files\7zip\7z.exe a d:\newFileName.7z "d:\ExistingFile.txt"

Lorusso answered 12/4, 2018 at 11:39 Comment(0)
T
0

Since 7-zip version 9.25 alpha there is a new -spf switch that can be used to store the full file paths including drive letter to the archive.

7zG.exe a -spf c:\BAckup\backup.zip @c:\temp\tmpFileList.txt

should be working just fine now.

Tailstock answered 20/6, 2017 at 6:5 Comment(0)
A
0

7-Zip wants relative paths in the list file otherwise it will store only the filenames, causing duplicate file name error.

Assuming that your list contains full path names:

  • Edit the list file to remove drive prefix, C:\
  • Make sure you are in the root of the drive when you run 7Z to use the above list file.
  • Then it will store the paths and won't complain of the duplicate name. It wants relative paths in the list file.

If your list file has paths relative to another folder, you should be running 7Z from that folder.

Update: I noticed from another post above that the new 7-Zip has an -spf option that doesn't require the above steps. Not tested it yet but my steps are for earlier versions that do not have this option.

Ancilin answered 25/8, 2017 at 5:20 Comment(0)
G
0

I created the following batch file to utilize 7-Zip to zip up every subfolder into its own zip file, preserving the file structure. Since this demonstrates the commandline effectively, I thought it might help someone else to post it here.

@ECHO OFF
REM Cycle Through all of the directories in the current folder
for /D %%d in (*) do (
    REM Change to the directory
    pushd %%d
    REM Make the destination directory.
    mkdir "Z:\%%d"

    REM Cycle Through all Subdirectories
    for /D %%b in (*) do (
        REM Zip up all subdirectores, and put it in the destination.
        7z -r a "Z:\%%d\%%b.zip" %%b
    )
    popd
)
Galatea answered 1/9, 2022 at 18:36 Comment(0)
S
-1

Instead of the option a use option x, this will create the directories but only for extraction, not compression.

Stheno answered 12/7, 2011 at 15:3 Comment(1)
Not relevant; the OP asked about storing paths in the file on compression, not on decompression.Stalag

© 2022 - 2024 — McMap. All rights reserved.