How to compress all files in a directory using 7-zip? - powershell

How do you archive all files (including ones without extensions) in a directory without archiving of the subdirectories using 7-zip command tool (7z.exe)?
If I use the wildcard with a dot (*.*) it works but only for files with extensions. Using * (or not using the wildcard at all) does not work even with the -r- switch (which is supposed to be then default according to the help file), since it archives everything in the source directory recursively. I just need to archive all files (some may not have extensions) in the immediate directory (no subfolders and no recursion). Any ideas?
The following commands do not work:
7z.exe a c:\output.7z c:\input -r-
7z.exe a c:\output.7z c:\input\* -r-
I am calling 7-zip from a PowerShell script, if this matters.

7-Zip will take an array of file objects for the input, so this should work:
& 7z.exe a C:\output.7z (Get-ChildItem C:\Input\* -File)

Related

How do I use a UNC path in a powershell script?

I have some powershell code that calls WinZip and makes an archive.
$program = "c:\program files (x86)\WinZip\WZZIP.EXE"
& $program -a -P -r c:\temp\test.zip ("\\server.domain.com\FirstName LastName\ProjectId\*.*")
The two backslashes are part of a UNC path. I must use UNC and a fully qualified domain name.
The above code attempts to run but hangs until I kill it (press the stop button in ISE).
When I wrap the UNC path in parenthesis, again the code attempts to run but hangs.
How do I fix this? I'm stuck with UNC paths and using WinZip.
UNC paths are not supported in WinZip from the command line. You will need to map a drive letter to the UNC path before preforming the zip. You can easily script this:
net use s: "\\server.domain.com\FirstName LastName\ProjectId\"
Alternatively, you can copy the files locally, zip them, then delete them from the local copy leaving only your zip file.
I believe 7zip does not have this limitation and is generally capable of doing everything WinZip is and more.
While not a direct answer to your question, perhaps you could use the PowerShell module PowerShellZIP at CodePlex (http://powershellzip.codeplex.com/) to create the ZIP files for you.
Then you can use the full powershell pipeline to create the ZIP files rather than calling out to WinZip.
I haven't tested to see if this module can zip files from an UNC path tho..

Winrar CommandLine & Powershell : Exclude Full Directory Path Structure

Suppose I have a directory structure like
C:\Users\Desktop\abc\d
I want to rar archive the abc folder so that the structure of rar is:
abc\d
When I try using powershell to archive, winrar replicates the full path inside the archive, like:
\Users\Desktop\abc\d
I dont want the full path to be created inside the archive
Here's the script:
https://gist.github.com/saurabhwahile/50f1091fb29c2bb327b7
What am I doing wrong?
Use the command line:
Rar.exe a -r -ep1 Test.rar "C:\Users\Desktop\abc"
Rar.exe is the console version of WinRAR stored in same directory as WinRAR.exe. You can use this command line also with WinRAR.exe if you want to see the compression process in a graphic window.
a is the command and means add files to archive.
-r is a switch to recursively add all files and subdirectories including empty subdirectories to the archive.
-ep1 is another switch which results in execluding base directory.
For this command line the base directory is "C:\Users\Desktop\" and therefore the created archive Test.rar contains only abc and all files and subdirectories in directory abc which is what you want.
One more hint: Using the command line
Rar.exe a -r -ep1 Test.rar "C:\Users\Desktop\abc\"
results in adding all files and subdirectories of directory abc to the archive, but without directory name abc being also stored in the archive. The backslash at end makes this difference.
In other words: On using switch -ep1 everything up to last backslash in file/directory specification is not added to the archive.
For more information about available switches see the text file Rar.txt in the program files directory of WinRAR.

How to partially extract a folder from a 7z file using powershell

I'm trying to automate the install of my platform. I've made a script for compressing the build of the deployables to a 7zip file.
Now i need to uncompress partially some folders to a specific destination.
Package
-app1
--folder11
---folder111
--folder12
-app2
--folder21
--folder22
...
I need to create a powershell script to extract the content of 'app1' to a destination folder.
I've been trying to use the following command but the result is not the as i expected.
I've been receiving the full path and not the content from folder11 recursivelly.
Set-Alias zip $ZipCommand
zip x $FilePath app1\folder11 -oc:DeployableFolder -r
Any ideas? Suggestions?
Thanks.
I tried and had no issue.
set-alias zip "c:\Program Files\outils\7-Zip\7z.exe"
zip x program.7z python-core-2.6.1\lib -oc:\data
I eventually got a c:\data\python-core-2.6.1 which only contains the lib folder with all its subfolders & files.
The only difference I see is the backslash \ in the output path.
HTH

Compressing only files using 7z without preserving the path

I am using 7z command line executable to zip files, but I see that while adding to an archive the path of the files is preserved in the archive.
So if I do
7z a -tzip myzip.zip dir1\dir2\*
the archive myzip.zip will contain the path dir1\dir2. I do not want this, rather I want only the files to be added to the zip file without the paths being preserved.
I searched quite a bit but do not seem to find any way of doing this, maybe I am missing something obvious?
Thanks
Just add a dot before the path, i.e.
7z a -tzip -r myzip.zip .\Relative\Dir\*
Give the full path. That should work. Not the relative path from the current location.
For example, I give the below, where I want the files in the man5 folder to be archived.
$ 7z a -tzip myzip.zip /home/pradeeban/Desktop/man4/man5/*
The zip contained only the files, without the directories.
Then I gave only the relative path. It had the directories, inside the zip.
$ 7z a -tzip myzip.zip Desktop/man4/man5/*
Tried with Linux (Ubuntu 12.04). Not sure whether that differs from Windows.
I discovered a way to do this by using a relative path:
7z a -tzip myzip.zip %CD%\dir1\dir2\*
%CD% is how you get the current path in a Windows batch file, but it also works from the command line. More info about Capturing the current directory from a batch file.
As explained in related question in 7-zip user FAQ, 7z stores paths relative to working directory, so you will need to first cd to desired top-level directory for archive and run 7-zip from here.
cd dir1\dir2\
7z a -tzip myzip.zip *
If you run it from script and don't want to affect it with changed directory, use directory push/pop facilities available in your shell of choice or run cd+7-zip in spawned process to avoid affecting your entire script with changed directory. For example, using Windows' start that would be:
start /D dir1\dir2\ /wait 7z a -tzip myzip.zip *
This worked for me
Consider folder structure like C:\Parent\SubFolders..... And you want to create parent.zip which will contain all files and folders C:\Parent without parent folder [i.e it will start from SubFolders.....]
cd /D "C:\Parent"
"7z.exe" a Parent.zip "*.*" -r
This will create Parent.zip in C:\Parent

How to extract ZIP files with WinRAR command line?

While trying to extract zip files I get the error:
c:\path\name.zip is not RAR archive
No files to extract
My code is:
p.StartInfo.FileName = #"C:\Program Files\WinRAR\rar.exe";
p.StartInfo.Arguments = string.Format("x -o- {2} \"{0}\" * \"{1}\"\\ ",
szFN,
outFolder,
passWord == null ? "" : string.Format("-p\"{0}\"", passWord));
The GUI version can extract zip and 7z files.
Why doesn't this work? How can I extract zip and 7z files?
(NOTE: I have different source code for 7zip. I guess I can merge the two and only use the above when the file has a rar extension. But I don't like that solution.)
Free unrar.exe and console versionRar.exe of WinRAR support only RAR archive format. That is clearly described in second paragraph in manual for Rar.exe which is the text file Rar.txt in program files folder of WinRAR.
You need to use WinRar.exe instead which supports also other archive formats:
[path\winrar.exe] x [switches] [path to zip file] [files to extract, . for all files] [path folder to extract to]
Example:
"%ProgramFiles%\WinRAR\winrar.exe" x -ibck c:\file.zip *.* c:\folder\
The syntax, commands and switches for GUI version WinRAR.exe are listed and described in help of WinRAR. Click in menu Help on menu item Help topics, open on help tab Contents the item Command line mode and read the help pages listed under this item.
For example the switch -ibck supported only by WinRAR.exe but not by Rar.exe is for running the extraction in background which means GUI version of WinRAR makes the extraction minimized to an icon in Windows system tray.
rar.exe can indeed only unpack rar files. It's not at all the same as WinRAR.
For unpacking ZIP files in .NET, you might want to look at the DotNetZip library instead. It has a license compatible with commercial software, unlike CSharpZipLib.
If you need to support RAR as well, you can use UnRAR.dll with pinvoke:
http://www.rarlab.com/rar_add.htm
http://www.rarlab.com/rar/UnRARDLL.exe
Or this .NET unRAR libary:
http://www.chilkatsoft.com/rar-dotnet.asp
Perhaps this one for 7zip.
You can use either SevenZipSharp or DotNetZip Library in your Application!
But I will go for SevenZipSharp Lib as It supports all the archives supported by 7-Zip!
Both Source and Binary are available in the Links!
for /f "tokens=*" %G in ('dir /on /b "D:\BACKUP_DATI\EXCEL\OPER*.ZIP"') do "C:\Program Files\7-Zip\7z.exe" x "..\%G" –aoa
References to further reading:
7-Zip command line examples
FOR /D - Loop through directory