s c h e m a t i c s : c o o k b o o k

/ Cookbook.FileReadZip

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Reading Data from ZIP Files

Problem

You have an archive in ZIP format and you want to access the files in the archive.

Solution

(require (planet "unzip.ss" ("dherman" "zip.plt" 2)))
(parameterize ([current-directory "output"])
  (with-input-from-file (build-path 'up "archive.zip") unzip))

Discussion

The zip.plt library in PLaneT has functionality for both unzipping an entire ZIP file or extracting individual entries. The above example unzips the entire contents of archive.zip to the output subdirectory of the current directory.

Here is an example that extracts a particular entry and saves it elsewhere:

(require (planet "unzip.ss" ("dherman" "zip.plt" 2)))
(require (lib "port.ss"))
(let ([zip-dir (read-zip-directory "archive.zip")])
  (unzip-entry "archive.zip"
               zip-dir
               #"subdir/document.txt"
               (lambda (name dir? in)
                 (with-output-to-file "local-copy.txt"
                   (copy-port in (current-output-port))))))

To extract an individual entry from a ZIP file, you first read its central directory with read-zip-directory, and then you extract the entry with unzip-entry. (Notice that entry names are specified with byte strings.)

See the PLaneT repository documentation for more information on the zip.plt library.


Comments about this recipe

Contributors

-- GordonWeakliem - 13 Aug 2004 -- DaveHerman - 22 Feb 2005

CookbookForm
TopicType: RecipeStub
ParentTopic: FileRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest