Compiling 7-zip (as 32 bit) on Windows 7 64bit with Visual Studio 10

(by Brian Wilson, 4/26/2012)


(Read a personal description of Backblaze here.)

0. What is 7-zip and why would I use it?

The program 7-zip unpacks and packs "ZIP Files" really well, and it's totally free.  The "ZIP File Format" is a way of bundling several files together into one file for convenience (in my company's case we use it to bundle all the files you want to restore from your online backup into one downloadable package).  ZIP is the most universal format on the planet for this, it is supported by Macintosh and Microsoft Windows right out of the box, no additional software needed.  However, the built in support on Microsoft (even Windows 7 64bit fully patched) falls short when you have either: A) non-US-ASCII characters in your filenames, or B) a ZIP file larger than 2 GBytes.  The ZIP format completely and totally handles these two things and it is totally standard, but Microsoft's implementation is inexplicably broken in these two very common cases.  The Macintosh fully supports these two cases right out of the box with no additional software, and all decent third party ZIP programs like WinRAR and 7-zip support these two issue flawlessly on Windows.  The advantage of 7-zip is that it is completely free and works great.  The official 7-zip website is here: http://www.7-zip.org/  I am just a fan of using this technology, I'm not associated with the official website or authors at all.

1. Shameless Adoration:

A free, cross-platform (Windows, Macintosh, Linux!) redistributable program, based on YEARS of work by these programmers for no apparent gain, fully redistributable world-wide!!  I thank the authors who worked on this from the bottom of my heart!  Thanks for fixing a short-coming on Windows!  -- Brian Wilson, 4/26/2012

2. The Nitty Gritty - Steps to Compile 7-Zip in Visual Studio 10

Requirements not covered in this webpage:

1) Microsoft Visual Studio 10
2) A "bunzip2" (for the ".bz2" file format) and "tar" tool that aren't standard on Microsoft Windows

2 (Step A). Download and unpack the source code from http://sourceforge.net/projects/sevenzip/files/

If you visit http://sourceforge.net/projects/sevenzip/files/ in a web browser, you will see two items: 1) "LZMA SDK" and 2) "7-Zip".  We want the "7-Zip" so click on that, and you should see a list of versions.  These instructions worked for Windows 7 64bit on 4/26/2012 for version 9.22 of 7-Zip.   The file you want is called 7z922.tar.bz2 and you should save it on C:\my7zip\7z922.tar.bz2 on your local disk.

Below are the commands I typed in a completely standard command prompt (cmd.exe) window.  DO NOT USE cygwin shells, they will not work!!

    2A.1 bunzip2.exe -d 7z922.tar.bz2
    2A.2 tar -xvf 7z922.tar

    2A.3 cd CPP   (the rest of the commands are done INSIDE here)

2 (Step B). Get Visual Studio environment setup (get "ml.exe" and "nmake" in your path)

The default install for Microsoft Visual Studio 10 doesn't include the environment variables to run Visual Studio from the command line.  The authors of 7-Zip prefer to work from the command line, so we adapt here.  You only really need two little executables called "ml.exe" and "nmake" which ship with Visual Studio, but here are the commands I typed to set this up.   Below are the commands I typed (remember, these are in the same completely standard Windows command prompt, cygwin shells will not work for this!!)

    2B.1 "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"         
(Notice the double quotes to help this run correctly.)

2 (Step C). Fix the Source Code - THE MOST FAMOUS 7-ZIP PROBLEM

Hilariously, the 7-Zip authors do a fabulous job but leave one problem in their Windows build files.  Everybody knows to edit it out.  The error is in a file called "C:\my7zip\CPP\Build.mak" and you need to remove this string "-OPT:NOWIN98" from line 34:

    2C.1 vi C:\my7zip\CPP\Build.mak         
Remove "-OPT:NOWIN98" from line 34 see below)
OLD_BAD_LINE-> LFLAGS = $(LFLAGS) -OPT:NOWIN98
NEW_CORRECT-> LFLAGS = $(LFLAGS)
 

2 (Step D). Build it!  This results in a 32 bit binary that will run on any Windows XP or later system

Below are the commands I typed to build OpenSSL with Visual Studio 10

    2D.1 cd C:\my7zip\CPP\7zip\
    2D.2 nmake NEW_COMPILER=1 MY_STATIC_LINK=1

2 (Step E). Copy the resulting stand along binary out of the Source Tree

I don't fully understand all the binaries that are produced.  PLEASE EMAIL ME IF YOU UNDERSTAND THIS!!!  (Update 7/29/2014 - thanks to Christoph Weber!  Some names filled in).  Here are some files produced:

  C:\my7zip\CPP\7zip\Bundles\Alone\O\7za.exe - ***** THIS IS THE BEST ONE!!  It understands ZIP files!
  C:\my7zip\CPP\7zip\Bundles\Alone7z\O\7zr.exe - totally unknown what this does??
  C:\my7zip\CPP\7zip\Bundles\Fm\O\7FM.exe - FM often stands for "Frequency Modulation" in radio, but this is a mystery - Update 7/29/2014 - Christoph says "7zFM means 'File Manager' and is the windows gui version which allows seamless navigation in the filesystem and archives."
  C:\my7zip\CPP\7zip\Bundles\LzmaCon\O\lmza.exe - LZMA is a common form of compression found in ZIP files - but this executable is a mystery
  C:\my7zip\CPP\7zip\UI\Client7z\O\7z.exe - When you run this one, it says "Can not load 7-zip library" - so this is bad
  C:\my7zip\CPP\7zip\UI\Console\O\7z.exe - When you run this one, it says "7-Zip cannot find the code that works with archives."
  C:\my7zip\CPP\7zip\UI\FileManager\O\7zFM.exe - FM often stands for "Frequency Modulation" in radio, but this is a double mystery
  C:\my7zip\CPP\7zip\UI\GUI\O\7z.exe - This pops up errors in the GUI on Windows instead of listing them in output - but I don't know what the params should be to it (it does not accept "-h" so the authors are weak on "ease of use")

Anyway, the best one from above is "C:\my7zip\CPP\7zip\UI\Console\O\7z.exe". Below are the commands I typed to copy it to my "C:\bin\" folder so I can use it:

    2E.1 copy C:\my7zip\CPP\7zip\Bundles\Alone\O\7za.exe C:\bin\7za.exe
(This just happens to be where I put useful utilities on my system.) align="left">
    3.1 cd C:\tmp
(Leave the source directory)
    3.2 C:\bin\7za.exe l C:\tmp\foo.zip
(This lists the contents of foo.zip)
    3.3 C:\bin\7za.exe x C:\tmp\foo.zip
(This extracts the contents of foo.zip)

ADDITIONAL HELPFUL HINTS FROM CHRISTOPH WEBER:

Some more hints can be found in the current source code archive in \DOC\readme.txt there is an overview of the folders:

Bundle Modules that are bundles of other modules
 *Executables
    Alone 7za.exe: Standalone version of 7z (Command Line )
    Alone7z 7zr.exe: Standalone version of 7z that supports only 7z/LZMA/BCJ/BCJ2 (command line)
    Fm\ 7FM.exe Standalone version of 7z (Win32 GUI)
    LzmaCon\ lmza.exe: pure-lzma-compressor without container format. Can only compress a single file like gzip

  *sfx-Modules (Self-Extractor)
    SFXCon 7zCon.sfx: Console 7z SFX module
    SFXWin 7z.sfx: Windows 7z SFX module
    SFXSetup 7zS.sfx: Windows 7z SFX module for Installers

  *Link-Library-DLLs for linking into your software
    Format7z 7za.dll: .7z support
    Format7zExtract 7zxa.dll: .7z support, extracting only
    Format7zR 7zr.dll: .7z support, LZMA/BCJ* only
    Format7zExtractR 7zxr.dll: .7z support, LZMA/BCJ* only, extracting only
    Format7zF 7z.dll: all formats

  UI\Client7z 7z.exe: command line version using dll. - This one is missing one of the Dlls from above (e.g. 7z.dll), you have to keep them together. It has to be named 7z.dll, even if you would
choose a different version like 7zxa.dll

  UI\Console 7z.exe: same as Client7z

  \UI\GUI\ 7zG.exe: same as UI\Console\7z.exe but compiled for windows gui mode (opens no command window). Same parameters. 7zG.exe a test.7z file1 file2...

	

All done!


(Read a personal description of Backblaze here.)

Click Here to return to Ski-Epic Home, and Click Here to return to Random Stuff in Brian's Life