[glib/resources] Support compressed files in resources



commit f3065e64e0d725dccaff4c3a7e658e850fec1d8d
Author: Alexander Larsson <alexl redhat com>
Date:   Thu Dec 22 10:10:37 2011 +0100

    Support compressed files in resources

 gio/Makefile.am              |    6 +++++-
 gio/glib-compile-resources.c |   41 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 3 deletions(-)
---
diff --git a/gio/Makefile.am b/gio/Makefile.am
index 1ce2cb2..813ed71 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -635,7 +635,11 @@ gio-2.0.lib: libgio-2.0.la gio.def
 
 bin_PROGRAMS = gio-querymodules glib-compile-schemas glib-compile-resources gsettings
 
-glib_compile_resources_LDADD = $(top_builddir)/glib/libglib-2.0.la
+glib_compile_resources_LDADD = \
+	$(top_builddir)/glib/libglib-2.0.la \
+	$(top_builddir)/gobject/libgobject-2.0.la	\
+	libgio-2.0.la
+
 glib_compile_resources_SOURCES = \
 	gvdb/gvdb-format.h		\
 	gvdb/gvdb-builder.h		\
diff --git a/gio/glib-compile-resources.c b/gio/glib-compile-resources.c
index f60ee87..51b1292 100644
--- a/gio/glib-compile-resources.c
+++ b/gio/glib-compile-resources.c
@@ -29,6 +29,10 @@
 #include <stdio.h>
 #include <locale.h>
 
+#include <gio/gmemoryoutputstream.h>
+#include <gio/gzlibcompressor.h>
+#include <gio/gconverteroutputstream.h>
+
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -40,6 +44,7 @@ typedef struct
 {
   char *content;
   gsize content_size;
+  gsize size;
   guint32 flags;
 } FileData;
 
@@ -156,7 +161,6 @@ get_parent (GHashTable *table,
   return parent;
 }
 
-
 static void
 end_element (GMarkupParseContext  *context,
 	     const gchar          *element_name,
@@ -230,6 +234,37 @@ end_element (GMarkupParseContext  *context,
 	  g_clear_error (&my_error);
 	  return;
 	}
+      data->size = data->content_size;
+
+      if (state->compressed)
+	{
+	  GOutputStream *out = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+	  GZlibCompressor *compressor =
+	    g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB, 9);
+	  GOutputStream *out2 = g_converter_output_stream_new (out, G_CONVERTER (compressor));
+
+	  if (!g_output_stream_write_all (out2, data->content, data->content_size,
+					  NULL, NULL, NULL) ||
+	      !g_output_stream_close (out2, NULL, NULL))
+	    {
+	      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
+			   _("Error compressing file %s"),
+			   real_file);
+	      return;
+	    }
+
+	  g_free (data->content);
+	  data->content_size = g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (out));
+	  data->content = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (out));
+
+	  g_object_unref (compressor);
+	  g_object_unref (out);
+	  g_object_unref (out2);
+
+	  data->flags |= G_RESOURCE_FLAGS_COMPRESSED;
+	}
+
+      g_free (real_file);
 
       g_hash_table_insert (state->table, key, data);
 
@@ -325,7 +360,7 @@ parse_resource_file (const gchar *filename)
 
 	  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(uuay)"));
 
-	  g_variant_builder_add (&builder, "u", data->content_size); /* Size */
+	  g_variant_builder_add (&builder, "u", data->size); /* Size */
 	  g_variant_builder_add (&builder, "u", data->flags); /* Flags */
 
 	  v_data = g_variant_new_from_data (G_VARIANT_TYPE("ay"),
@@ -397,6 +432,8 @@ main (int argc, char **argv)
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 #endif
 
+  g_type_init ();
+
   context = g_option_context_new (N_("FILE"));
   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
   g_option_context_set_summary (context,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]