1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * test-cp-zip.c: Test gsf-outfile-zip by cloning a file the hard way
5 * Copyright (C) 2002-2006 Jody Goldberg (jody@gnome.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2.1 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 #include <gsf/gsf-utils.h>
24 #include <gsf/gsf-input-stdio.h>
25 #include <gsf/gsf-infile.h>
26 #include <gsf/gsf-infile-zip.h>
28 #include <gsf/gsf-output-stdio.h>
29 #include <gsf/gsf-outfile.h>
30 #include <gsf/gsf-outfile-zip.h>
35 clone (GsfInfile *in, GsfOutfile *out)
37 GsfInput *input = GSF_INPUT (in);
38 GsfOutput *output = GSF_OUTPUT (out);
40 if (gsf_input_size (input) > 0) {
42 while ((len = gsf_input_remaining (input)) > 0) {
44 /* copy in odd sized chunks to exercise system */
47 if (NULL == (data = gsf_input_read (input, len, NULL))) {
48 g_warning ("error reading ?");
51 if (!gsf_output_write (output, len, data)) {
52 g_warning ("error writing ?");
57 int i, n = gsf_infile_num_children (in);
58 for (i = 0 ; i < n; i++) {
61 char const *name = gsf_infile_name_by_index (in, i);
62 char *display_name = name
63 ? g_filename_display_name (name)
66 input = gsf_infile_child_by_index (in, i);
68 g_print ("Error opening '%s, index = %d\n",
69 display_name ? display_name : "?", i);
70 g_free (display_name);
74 is_dir = gsf_infile_num_children (GSF_INFILE (input)) >= 0;
76 g_object_get (G_OBJECT (input), "compression-level", &level, NULL);
78 g_print ("%s: size=%ld, level=%d, %s\n",
79 display_name ? display_name : "?",
80 (long)gsf_input_size (input),
82 is_dir ? "directory" : "file");
83 g_free (display_name);
85 output = gsf_outfile_new_child_full (out, name, is_dir,
86 "compression-level", level,
88 clone (GSF_INFILE (input), GSF_OUTFILE (output));
91 gsf_output_close (GSF_OUTPUT (out));
92 g_object_unref (G_OBJECT (out));
93 g_object_unref (G_OBJECT (in));
105 fprintf (stderr, "%s\n", argv [1]);
106 input = gsf_input_stdio_new (argv[1], &err);
109 g_return_val_if_fail (err != NULL, 1);
111 g_warning ("'%s' error: %s", argv[1], err->message);
116 infile = gsf_infile_zip_new (input, &err);
117 g_object_unref (G_OBJECT (input));
119 if (infile == NULL) {
120 g_return_val_if_fail (err != NULL, 1);
122 g_warning ("'%s' Not a zip file: %s", argv[1], err->message);
127 output = gsf_output_stdio_new (argv[2], &err);
128 if (output == NULL) {
130 g_return_val_if_fail (err != NULL, 1);
132 g_warning ("'%s' error: %s", argv[2], err->message);
134 g_object_unref (G_OBJECT (infile));
138 outfile = gsf_outfile_zip_new (output, &err);
139 g_object_unref (G_OBJECT (output));
140 clone (infile, outfile);
146 main (int argc, char *argv[])
151 fprintf (stderr, "%s : infile outfile\n", argv [0]);