file/unzip: add help func with-unzip with-unzip-entry

This commit is contained in:
simmone 2014-02-09 17:08:23 +08:00 committed by Matthew Flatt
parent 351545a10a
commit f592cf6e35
4 changed files with 87 additions and 0 deletions

View File

@ -30,6 +30,20 @@ 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?]{
A helper func to wrap unzip with clean temporay unzip files.
Unzips an entire @exec{zip} archive from file path @racket[zip_file] to a temporary directory.
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]
[#:strip-count strip-count exact-nonnegative-integer? 0]
@ -137,6 +151,22 @@ 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?]{
A helper func to wrap unzip-entry with clean temporay unzip file.
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.}
@defproc[(path->zip-path [path path-string?]) bytes?]{
Converts a file name potentially containing path separators in the current

Binary file not shown.

View File

@ -0,0 +1,26 @@
#lang racket/base
(require file/unzip tests/eli-tester)
;; 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-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"))))))
(define (run-tests)
(test-with-unzip)
(test-with-unzip-entry))
(provide tests)
(module+ main (tests))
(define (tests) (test do (run-tests)))

View File

@ -43,6 +43,9 @@
. ->* .
any)]
[with-unzip-entry (-> path-string? path-string? (-> path-string? any) any)]
[with-unzip (-> path-string? (-> path-string? any) any)]
[path->zip-path ((or/c string? path?) . -> . bytes?)]))
;; ===========================================================================
@ -359,3 +362,31 @@
(if (path? base)
base
(current-directory)))
;; use dynamic-wind to clean temporary files
(define (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 ".")))
(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))))
(lambda ()
(delete-directory/files temp_dir)))))
(define (with-unzip zip_file user_proc)
(let ([temp_dir #f])
(dynamic-wind
(lambda ()
(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))
(lambda ()
(delete-directory/files temp_dir)))))