Script to make Windows XP-readable ZIP file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pac

    Script to make Windows XP-readable ZIP file

    I'm preparing to distribute a Windows XP Python program and some
    ancillary files,
    and I wanted to put everything in a .ZIP archive. It proved to be
    inordinately
    difficult and I thought I would post my solution here. Is there a
    better one?

    Suppose you have a set of files in a directory c:\a\b and some
    additional
    files in c:\a\b\subdir. Using a Python script, you would
    like to make a Windows-readable archive (.zip) that preserves this
    directory structure, and where the root directory of the archive is
    c:\a\b. In other words, all the files from c:\a\b appear in the
    archive
    without a path prefix, and all the files in c:\a\b\subdir have a path
    prefix of \subdir.

    This looks like it should be easy with module zipfile and the handy
    function os.walk. Create a zip file, call os.walk, and add the files
    to
    the archive like so:

    import os
    import zipfile

    z =
    zipfile.ZipFile (r"c:\a\b\myzip .zip",mode="w", compression=zip file.ZIP_DEFLAT ED)

    for dirpath,dirs,fi les in os.walk(r"c:\a\ b"):
    for a_file in files:
    a_path = os.path.join(di rpath,a_file)
    z.write(a_path) # Change, see below
    z.close()

    This creates an archive that can be read by WinZip or by another Python
    script
    that uses zipfile. But when you try to view it with the Windows
    compressed folder
    viewer it will appear empty. If you try to extract the files anyway
    (because
    you know they are really there), you get a Windows Security Warning and
    XP
    refuses to decompress the folder - XP is apparently afraid it might be
    bird flu
    or something.

    If you change the line marked #Change to "z.write(a_path ,file)",
    explicitly naming
    each file, now the compressed folder viewer will show all the files in
    the archive.
    XP will not treat it like a virus and it will extract the files.
    However, the
    archive does not contain a subdirectory; all the files are in a single
    directory.

    Some experimentation suggests that Windows does not like any filename
    in the
    archive that begins with either a drive designator like c:, or has a
    path containing
    a leading slash like "\a\b\afile.txt ". Relative paths like
    "subdir\afile.t xt" are
    okay, and cause the desired behavior when the archive is extracted,
    e.g., a new directory subdir is created and afile.txt is placed in it.

    Since the method ZipFile.write needs a valid pathname for each file,
    the correct
    solution to the original problem entails messing around with the OS's
    current
    working directory. Position the CWD in the desired base directory of
    the archive,
    add the files to the archive using their relative pathnames, and put
    the CWD back
    where it was when you started:

    import os
    import zipfile

    z =
    zipfile.ZipFile (r"c:\a\b\myzip .zip",mode="w", compression=zip file.ZIP_DEFLAT ED)

    cwd = os.getcwd()
    os.chdir(base_d ir)
    try:
    for dirpath,dirs,fi les in os.walk(''): # This starts the walk at
    the CWD
    for a_file in files:
    a_path = os.path.join(di rpath,a_file)
    z.write(a_path, a_path) # Can the second argument be
    omitted?
    z.close()
    finally:
    os.chdir(cwd)

    This produces an archive that can be extracted by Windows XP using its
    built-in
    capability, by WinZip, or by another Python script. Now that I have
    the solution it
    seems to make sense, but it wasn't at all obvious when I started.

    Paul Cornelius

  • softwindow

    #2
    Re: Script to make Windows XP-readable ZIP file

    i am in win2000
    "z.write(a_path ,a_path)" may change to "z.write(a_path )"
    but the dirpath is not in zipfile
    who can tell me?

    Comment

    • Carl Banks

      #3
      Re: Script to make Windows XP-readable ZIP file

      pac wrote:[color=blue]
      > Suppose you have a set of files in a directory c:\a\b and some
      > additional
      > files in c:\a\b\subdir. Using a Python script, you would
      > like to make a Windows-readable archive (.zip) that preserves this
      > directory structure, and where the root directory of the archive is
      > c:\a\b. In other words, all the files from c:\a\b appear in the
      > archive
      > without a path prefix, and all the files in c:\a\b\subdir have a path
      > prefix of \subdir.
      >
      > This looks like it should be easy with module zipfile and the handy
      > function os.walk. Create a zip file, call os.walk, and add the files
      > to
      > the archive like so:
      >
      > import os
      > import zipfile
      >
      > z =
      > zipfile.ZipFile (r"c:\a\b\myzip .zip",mode="w", compression=zip file.ZIP_DEFLAT ED)
      >
      > for dirpath,dirs,fi les in os.walk(r"c:\a\ b"):
      > for a_file in files:
      > a_path = os.path.join(di rpath,a_file)
      > z.write(a_path) # Change, see below
      > z.close()[/color]

      (Aside: be careful not to use tabs when posting. I suspect the f-bot
      will be here to tell you that the above code doesn't work.)
      [color=blue]
      > Some experimentation suggests that Windows does not like any filename
      > in the
      > archive that begins with either a drive designator like c:, or has a
      > path containing
      > a leading slash like "\a\b\afile.txt ". Relative paths like
      > "subdir\afile.t xt" are
      > okay, and cause the desired behavior when the archive is extracted,
      > e.g., a new directory subdir is created and afile.txt is placed in it.
      >
      > Since the method ZipFile.write needs a valid pathname for each file,
      > the correct
      > solution to the original problem entails messing around with the OS's
      > current
      > working directory.[/color]

      ZipFile.write takes an optional second argument for the archive
      filename. You could have done something like this (untested):

      for dirpath,dirs,fi les in os.walk(r"c:\a\ b"):
      for a_file in files:
      a_path = os.path.join(di rpath,a_file)
      z_path = a_path[7:] # or whatever
      z.write(a_path, z_path)
      z.close()

      And maybe use a little helper function instead of the string slice to
      make it more robust (it violates DRY, and I'm not happy to assume the
      dirpath returned by os.walk has exactly the same prefix as the
      argument).
      [color=blue]
      > Position the CWD in the desired base directory of
      > the archive,
      > add the files to the archive using their relative pathnames, and put
      > the CWD back
      > where it was when you started:[/color]

      This may be the best way anyways, unless you have some reason to not
      change the current directory.


      Carl Banks

      Comment

      • John Bokma

        #4
        Re: Script to make Windows XP-readable ZIP file

        "pac" <pac@fernside.c om> wrote:
        [color=blue]
        > I'm preparing to distribute a Windows XP Python program and some
        > ancillary files,
        > and I wanted to put everything in a .ZIP archive. It proved to be
        > inordinately
        > difficult and I thought I would post my solution here. Is there a
        > better one?[/color]

        heresy maybe, but I use Ant to do such things (with my Perl projects):


        --
        John MexIT: http://johnbokma.com/mexit/
        personal page: http://johnbokma.com/
        Experienced programmer available: http://castleamber.com/
        Happy Customers: http://castleamber.com/testimonials.html

        Comment

        • softwindow

          #5
          Re: Script to make Windows XP-readable ZIP file

          my code can work, like below:

          import os
          import zipfile
          z =
          zipfile.ZipFile (r"c:\text.zip" ,mode="w",compr ession=zipfile. ZIP_DEFLATED)
          cwd = os.getcwd()
          try:
          for dirpath,dirs,fi les in os.walk(cwd):
          for file in files:
          z_path = os.path.join(di rpath,file)
          z.write(z_path)
          z.close()
          finally:
          if z:
          z.close()

          that is true
          but the archive include the absolute path .
          can you give me a way to build it with relative path.

          Comment

          • softwindow

            #6
            Re: Script to make Windows XP-readable ZIP file

            aha
            we want to do it with python
            don't use ant

            Comment

            • John Bokma

              #7
              Re: Script to make Windows XP-readable ZIP file

              "softwindow " <softwindow@gma il.com> wrote:
              [color=blue]
              > aha
              > we want to do it with python
              > don't use ant[/color]

              :-D

              I want to do a lot with Perl, but sometimes it's better to use the right
              tool for the job. And I think that Ant is better at for what I use it
              compared to a home brew tool I could make. Be careful with "Not Invented
              Here".

              --
              John MexIT: http://johnbokma.com/mexit/
              personal page: http://johnbokma.com/
              Experienced programmer available: http://castleamber.com/
              Happy Customers: http://castleamber.com/testimonials.html

              Comment

              • softwindow

                #8
                Re: Script to make Windows XP-readable ZIP file

                Carl Banks is right

                Comment

                • softwindow

                  #9
                  Re: Script to make Windows XP-readable ZIP file

                  aha
                  now it's right like this:

                  import os
                  import zipfile
                  z =
                  zipfile.ZipFile (r"c:\text.zip" ,mode="w",compr ession=zipfile. ZIP_DEFLATED)
                  cwd = os.getcwd()
                  try:
                  for dirpath,dirs,fi les in os.walk(cwd):
                  for file in files:
                  z_path = os.path.join(di rpath,file)
                  start = cwd.rfind(os.se p)+1
                  z.write(z_path, z_path[start:])
                  z.close()
                  finally:
                  if z:
                  z.close()

                  Comment

                  • softwindow

                    #10
                    Re: Script to make Windows XP-readable ZIP file

                    import os
                    import zipfile
                    z =
                    zipfile.ZipFile (r"c:\text.zip" ,mode="w",compr ession=zipfile. ZIP_DEFLATED)
                    cwd = os.getcwd()
                    try:
                    for dirpath,dirs,fi les in os.walk(cwd):
                    for file in files:
                    z_path = os.path.join(di rpath,file)
                    start = cwd.rfind(os.se p)+1
                    z.write(z_path, z_path[start:])
                    z.close()
                    finally:
                    if z:
                    z.close()


                    *************** ******
                    can work

                    Comment

                    • John Bokma

                      #11
                      Re: Script to make Windows XP-readable ZIP file

                      "softwindow " <softwindow@gma il.com> wrote:
                      [color=blue]
                      > Carl Banks is right[/color]

                      Did he write to check out:
                      http://groups.google.com/support/bin...y?answer=14213 ?

                      Why didn't you do so?

                      --
                      John MexIT: http://johnbokma.com/mexit/
                      personal page: http://johnbokma.com/
                      Experienced programmer available: http://castleamber.com/
                      Happy Customers: http://castleamber.com/testimonials.html

                      Comment

                      • Grant Edwards

                        #12
                        Re: Script to make Windows XP-readable ZIP file

                        On 2006-05-19, softwindow <softwindow@gma il.com> wrote:
                        [color=blue]
                        > Carl Banks is right[/color]

                        That would be valuable information if we know what he was right
                        about.

                        --
                        Grant Edwards grante Yow! Bo Derek ruined
                        at my life!
                        visi.com

                        Comment

                        • Larry Bates

                          #13
                          Re: Script to make Windows XP-readable ZIP file

                          pac wrote:[color=blue]
                          > I'm preparing to distribute a Windows XP Python program and some
                          > ancillary files,
                          > and I wanted to put everything in a .ZIP archive. It proved to be
                          > inordinately
                          > difficult and I thought I would post my solution here. Is there a
                          > better one?
                          >
                          > Suppose you have a set of files in a directory c:\a\b and some
                          > additional
                          > files in c:\a\b\subdir. Using a Python script, you would
                          > like to make a Windows-readable archive (.zip) that preserves this
                          > directory structure, and where the root directory of the archive is
                          > c:\a\b. In other words, all the files from c:\a\b appear in the
                          > archive
                          > without a path prefix, and all the files in c:\a\b\subdir have a path
                          > prefix of \subdir.
                          >
                          > This looks like it should be easy with module zipfile and the handy
                          > function os.walk. Create a zip file, call os.walk, and add the files
                          > to
                          > the archive like so:
                          >
                          > import os
                          > import zipfile
                          >
                          > z =
                          > zipfile.ZipFile (r"c:\a\b\myzip .zip",mode="w", compression=zip file.ZIP_DEFLAT ED)
                          >
                          > for dirpath,dirs,fi les in os.walk(r"c:\a\ b"):
                          > for a_file in files:
                          > a_path = os.path.join(di rpath,a_file)
                          > z.write(a_path) # Change, see below
                          > z.close()
                          >
                          > This creates an archive that can be read by WinZip or by another Python
                          > script
                          > that uses zipfile. But when you try to view it with the Windows
                          > compressed folder
                          > viewer it will appear empty. If you try to extract the files anyway
                          > (because
                          > you know they are really there), you get a Windows Security Warning and
                          > XP
                          > refuses to decompress the folder - XP is apparently afraid it might be
                          > bird flu
                          > or something.
                          >
                          > If you change the line marked #Change to "z.write(a_path ,file)",
                          > explicitly naming
                          > each file, now the compressed folder viewer will show all the files in
                          > the archive.
                          > XP will not treat it like a virus and it will extract the files.
                          > However, the
                          > archive does not contain a subdirectory; all the files are in a single
                          > directory.
                          >
                          > Some experimentation suggests that Windows does not like any filename
                          > in the
                          > archive that begins with either a drive designator like c:, or has a
                          > path containing
                          > a leading slash like "\a\b\afile.txt ". Relative paths like
                          > "subdir\afile.t xt" are
                          > okay, and cause the desired behavior when the archive is extracted,
                          > e.g., a new directory subdir is created and afile.txt is placed in it.
                          >
                          > Since the method ZipFile.write needs a valid pathname for each file,
                          > the correct
                          > solution to the original problem entails messing around with the OS's
                          > current
                          > working directory. Position the CWD in the desired base directory of
                          > the archive,
                          > add the files to the archive using their relative pathnames, and put
                          > the CWD back
                          > where it was when you started:
                          >
                          > import os
                          > import zipfile
                          >
                          > z =
                          > zipfile.ZipFile (r"c:\a\b\myzip .zip",mode="w", compression=zip file.ZIP_DEFLAT ED)
                          >
                          > cwd = os.getcwd()
                          > os.chdir(base_d ir)
                          > try:
                          > for dirpath,dirs,fi les in os.walk(''): # This starts the walk at
                          > the CWD
                          > for a_file in files:
                          > a_path = os.path.join(di rpath,a_file)
                          > z.write(a_path, a_path) # Can the second argument be
                          > omitted?
                          > z.close()
                          > finally:
                          > os.chdir(cwd)
                          >
                          > This produces an archive that can be extracted by Windows XP using its
                          > built-in
                          > capability, by WinZip, or by another Python script. Now that I have
                          > the solution it
                          > seems to make sense, but it wasn't at all obvious when I started.
                          >
                          > Paul Cornelius
                          >[/color]
                          Others have addressed your specific question, I wanted to make a
                          more general suggestion.

                          You should really take a look at Inno Installer and py2exe combination
                          for creating .zip library and Windows distribution. I PROMISE it will
                          be worth your time on future projects. Rolling your own installer will
                          take much more time/effort over the long haul.

                          -Larry Bates

                          Comment

                          Working...