From 61d717fd0782232d5ab9906af9ab75151a1e81e5 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Wed, 30 Apr 2014 17:46:26 -0600 Subject: [PATCH] file/unzip: change to `call-with-unzip` and `call-with-unzip-entry` Use `call-with-` instead of `with-`, since it's a function instead of a syntactic form. (This is a weak convention, but one to try to follow with new functions and forms.) Fix up the contracts and change the documentation to match the usual style. Create the temporary directory in `(find-system-path 'temp-dir)` instead of the current directory. --- .../racket-doc/file/scribblings/unzip.scrbl | 40 +++++++---------- .../racket-test/tests/file/unzip.rkt | 33 ++++++++------ racket/collects/file/unzip.rkt | 45 ++++++++++--------- 3 files changed, 60 insertions(+), 58 deletions(-) diff --git a/pkgs/racket-pkgs/racket-doc/file/scribblings/unzip.scrbl b/pkgs/racket-pkgs/racket-doc/file/scribblings/unzip.scrbl index b3dc8d99b5..d0f054f5e8 100644 --- a/pkgs/racket-pkgs/racket-doc/file/scribblings/unzip.scrbl +++ b/pkgs/racket-pkgs/racket-doc/file/scribblings/unzip.scrbl @@ -7,7 +7,7 @@ @defmodule[file/unzip]{The @racketmodname[file/unzip] library provides a function to extract items from a @exec{zip} archive.} -@defproc[(unzip [in (or/c path-string? input-port)] +@defproc[(unzip [in (or/c path-string? input-port?)] [entry-reader (if preserve-timestamps? (bytes? boolean? input-port? (or/c #f exact-integer?) . -> . any) @@ -30,19 +30,16 @@ aspects of the unpacking, such as the destination directory. @history[#:changed "6.0.0.3" @elem{Added the @racket[#:preserve-timestamps?] argument.}]} -@defproc[(with-unzip [zip_file path-string?] - [user_proc (-> path-string? any)]) - void?]{ +@defproc[(call-with-unzip [in (or/c path-string? input-port?)] + [proc (-> path-string? any)]) + any]{ -A helper func to wrap unzip with clean temporay unzip files. +Unpacks @racket[in] to a temporary directory, calls @racket[proc] on +the temporary directory's path, and then deletes the temporary +directory while returning the result of @racket[proc]. -Unzips an entire @exec{zip} archive from file path @racket[zip_file] to a temporary directory. +@history[#:added "6.0.1.6"]} -The temporary directory is created in the @racket[zip_file]'s directory. - -@racket[user_proc] has this temporary directory path as its parameter. - -When done, delete temporay unzip files and directories.} @defproc[(make-filesystem-entry-reader [#:dest dest-path (or/c path-string? #f) #f] @@ -151,20 +148,17 @@ If @racket[entry] is not in @racket[zipdir], an @history[#:changed "6.0.0.3" @elem{Added the @racket[#:preserve-timestamps?] argument.}]} -@defproc[(with-unzip-entry [zip_file path-string?] - [entry_file path-string?] - [user_proc (-> path-string? any)]) - void?]{ +@defproc[(call-with-unzip-entry [in path-string? input-port] + [entry path-string?] + [proc (-> path-string? any)]) + any]{ -A helper func to wrap unzip-entry with clean temporay unzip file. +Unpacks @racket[entry] within @racket[in] to a temporary directory, +calls @racket[proc] on the unpacked file's path, and then +deletes the temporary directory while returning the result of +@racket[proc]. -Unzip an specific entry from @racket[zip_file] to a temporary directory, @racket[entry_file] is a entry path in @racket[zip_file]. - -The temporary directory is created in the @racket[zip_file]'s directory. - -@racket[user_proc] has this unzipped temporary file path as its parameter. - -When done, delete temporay unzip file.} +@history[#:added "6.0.1.6"]} @defproc[(path->zip-path [path path-string?]) bytes?]{ diff --git a/pkgs/racket-pkgs/racket-test/tests/file/unzip.rkt b/pkgs/racket-pkgs/racket-test/tests/file/unzip.rkt index bf52baa9ca..0b8325dd76 100644 --- a/pkgs/racket-pkgs/racket-test/tests/file/unzip.rkt +++ b/pkgs/racket-pkgs/racket-test/tests/file/unzip.rkt @@ -1,24 +1,29 @@ #lang racket/base -(require file/unzip tests/eli-tester) +(require file/unzip + racket/runtime-path + tests/eli-tester) + +(define-runtime-path unzip-me.zip "unzip-me.zip") ;; test-me.zip's directory structure is test-zip/1/data.dat -(define (test-with-unzip) - (with-unzip "unzip-me.zip" - (lambda (tmp_dir) - (with-input-from-file (build-path tmp_dir "test-zip" "1" "data.dat") - (lambda () - (test (read-line) => "chenxiao")))))) +(define (test-with-unzip in) + (call-with-unzip in + (lambda (tmp_dir) + (with-input-from-file (build-path tmp_dir "test-zip" "1" "data.dat") + (lambda () + (test (read-line) => "chenxiao")))))) (define (test-with-unzip-entry) - (with-unzip-entry "unzip-me.zip" - (build-path "test-zip" "1" "data.dat") - (lambda (tmp_file) - (with-input-from-file tmp_file - (lambda () - (test (read-line) => "chenxiao")))))) + (call-with-unzip-entry unzip-me.zip + (build-path "test-zip" "1" "data.dat") + (lambda (tmp_file) + (with-input-from-file tmp_file + (lambda () + (test (read-line) => "chenxiao")))))) (define (run-tests) - (test-with-unzip) + (test-with-unzip unzip-me.zip) + (call-with-input-file* unzip-me.zip test-with-unzip) (test-with-unzip-entry)) (provide tests) diff --git a/racket/collects/file/unzip.rkt b/racket/collects/file/unzip.rkt index 21115ad0b0..8545923284 100644 --- a/racket/collects/file/unzip.rkt +++ b/racket/collects/file/unzip.rkt @@ -43,10 +43,15 @@ . ->* . any)] - [with-unzip-entry (-> path-string? path-string? (-> path-string? any) any)] - [with-unzip (-> path-string? (-> path-string? any) any)] + [call-with-unzip (-> (or/c path-string? input-port?) + (-> path-string? any) + any)] + [call-with-unzip-entry (-> (or/c path-string? input-port?) + path-string? + (-> path-string? any) + any)] - [path->zip-path ((or/c string? path?) . -> . bytes?)])) + [path->zip-path (path-string? . -> . bytes?)])) ;; =========================================================================== ;; CONSTANTS @@ -363,30 +368,28 @@ base (current-directory))) -;; use dynamic-wind to clean temporary files -(define (with-unzip-entry zip_file entry_file user_proc) - (let ([temp_dir #f]) +(define (call-with-unzip-entry zip-file entry-file user-proc) + (let ([temp-dir #f]) (dynamic-wind (lambda () - (set! temp_dir (make-temporary-file "ziptmp~a" 'directory "."))) + (set! temp-dir (make-temporary-file "ziptmp~a" 'directory))) (lambda () - (let ([directory_entries (read-zip-directory zip_file)]) - (unzip-entry - zip_file - directory_entries - (path->zip-path entry_file) - (make-filesystem-entry-reader #:dest temp_dir #:exists 'replace)) - (user_proc (build-path temp_dir entry_file)))) + (let ([directory-entries (read-zip-directory zip-file)]) + (unzip-entry zip-file + directory-entries + (path->zip-path entry-file) + (make-filesystem-entry-reader #:dest temp-dir #:exists 'replace)) + (user-proc (build-path temp-dir entry-file)))) (lambda () - (delete-directory/files temp_dir))))) + (delete-directory/files temp-dir))))) -(define (with-unzip zip_file user_proc) - (let ([temp_dir #f]) +(define (call-with-unzip zip-file user-proc) + (let ([temp-dir #f]) (dynamic-wind (lambda () - (set! temp_dir (make-temporary-file "ziptmp~a" 'directory "."))) + (set! temp-dir (make-temporary-file "ziptmp~a" 'directory))) (lambda () - (unzip zip_file (make-filesystem-entry-reader #:dest temp_dir #:exists 'replace)) - (user_proc temp_dir)) + (unzip zip-file (make-filesystem-entry-reader #:dest temp-dir #:exists 'replace)) + (user-proc temp-dir)) (lambda () - (delete-directory/files temp_dir))))) + (delete-directory/files temp-dir)))))