Thomas Klausner | e38e779 | 2016-09-12 12:13:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | fseek.c -- test tool for seeking in zip archives |
| 3 | Copyright (C) 2016 Dieter Baron and Thomas Klausner |
| 4 | |
| 5 | This file is part of libzip, a library to manipulate ZIP archives. |
| 6 | The authors can be contacted at <libzip@nih.at> |
| 7 | |
| 8 | Redistribution and use in source and binary forms, with or without |
| 9 | modification, are permitted provided that the following conditions |
| 10 | are met: |
| 11 | 1. Redistributions of source code must retain the above copyright |
| 12 | notice, this list of conditions and the following disclaimer. |
| 13 | 2. Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in |
| 15 | the documentation and/or other materials provided with the |
| 16 | distribution. |
| 17 | 3. The names of the authors may not be used to endorse or promote |
| 18 | products derived from this software without specific prior |
| 19 | written permission. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
| 22 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 25 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 27 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 29 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 30 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 31 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | |
| 36 | #include "zip.h" |
| 37 | |
| 38 | const char *prg; |
| 39 | #define USAGE "usage: %s archive index offset\n" |
| 40 | |
| 41 | int |
| 42 | main(int argc, char *argv[]) |
| 43 | { |
| 44 | int ze; |
| 45 | zip_t *z; |
| 46 | zip_file_t *zf; |
| 47 | char *archive; |
| 48 | zip_int64_t index, offset, n; |
| 49 | char b[1024]; |
| 50 | |
| 51 | prg = argv[0]; |
| 52 | |
| 53 | if (argc != 4) { |
| 54 | fprintf(stderr, USAGE, prg); |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | archive = argv[1]; |
| 59 | index = strtoull(argv[2], NULL, 10); |
| 60 | offset = strtoull(argv[3], NULL, 10); |
| 61 | |
| 62 | if ((z=zip_open(archive, 0, &ze)) == NULL) { |
| 63 | zip_error_t error; |
| 64 | zip_error_init_with_code(&error, ze); |
| 65 | fprintf(stderr, "%s: can't open zip archive '%s': %s\n", prg, archive, zip_error_strerror(&error)); |
| 66 | zip_error_fini(&error); |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | if ((zf=zip_fopen_index(z, index, 0)) == NULL) { |
| 71 | fprintf(stderr, "%s: can't open file in archive '%s': %s\n", prg, archive, zip_error_strerror(zip_file_get_error(zf))); |
| 72 | zip_close(z); |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | if (zip_fseek(zf, offset, SEEK_SET) < 0) { |
| 77 | fprintf(stderr, "%s: zip_fseek failed: %s\n", prg, zip_error_strerror(zip_file_get_error(zf))); |
| 78 | zip_close(z); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | while ((n=zip_fread(zf, b, sizeof(b))) > 0) { |
Thomas Klausner | aa7fba7 | 2016-09-16 16:18:29 +0200 | [diff] [blame] | 83 | printf("%.*s", (int)n, b); |
Thomas Klausner | e38e779 | 2016-09-12 12:13:34 +0200 | [diff] [blame] | 84 | } |
| 85 | if (n < 0) { |
| 86 | fprintf(stderr, "%s: zip_fread failed: %s\n", prg, zip_error_strerror(zip_file_get_error(zf))); |
| 87 | zip_close(z); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | if (zip_close(z) == -1) { |
| 92 | fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, archive, zip_strerror(z)); |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |