1 Dec
2011

Vala : example for writing archives using libarchive

I only found one example of how to use libarchive in Vala. Example for reading archives using libarchive

I am trying to make the code to write an archives in Vala using libarchive. I have translated from C to Vala. http://code.google.com/p/libarchive/wiki/ManPageArchiveWrite3

/* write-archive.vala
 *
 * Copyright (C) 2011  Aji Kisworo Mukti <adzy@di.blankon.in>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */
using Archive;
using Posix;
using GLib;

private const int BUFFER_SIZE = 4096;

void main (string[] args) {
	var archive = new Write ();
	var buffer = new uint8[BUFFER_SIZE];

	archive.set_compression_compress ();
	archive.set_format_ar_bsd ();

	if (archive.open_filename ("archive.ar") != Result.OK) {
		GLib.stderr.printf ("%s\n", archive.error_string ());
	}

	var entry = new Entry ();
	entry.set_pathname ("METADATA");
	Posix.Stat st;
	Posix.stat ("METADATA", out st);
	entry.copy_stat (st);
	entry.set_size (st.st_size);

	if (archive.write_header (entry) != Result.OK) {
		GLib.stderr.printf ("%s\n", archive.error_string ());
	}

	// write the file
	var file = File.new_for_path ("METADATA");
	var reader = file.read ();
	var len = reader.read (buffer);
	while(len > 0)
	{
		archive.write_data(buffer, len);
		len = reader.read (buffer);
	}

	if (archive.finish_entry () != Result.OK) {
		GLib.stderr.printf ("%s\n", archive.error_string ());
	}

	// close the archive
	if (archive.close () != Result.OK) {
		GLib.stderr.printf ("%s\n", archive.error_string ());
	}
}

 


Tell me about the problems and questions about my code :)