FilterCompressedFiles



Hello,

I've been playing a bit with beagle creating an small filter, this
look into some compressed files, and index the file names inside the
compressed file.

ie: If we have example.tgz which contains fileA.txt and fileB.txt, the
filter append the text 'fileA.txt fileB.txt', not the content of both
files. So, with this filter we can look for fileA and we will obtain
example.tgz as result.

Currently supports:
- tar
- tar + bzip2
- tar + gzip
- zip
- rar

But the support new extensions is really easy. Only a tool that list
the content of a compressed file is needed. If that tool, as occurs
with unzip, shows unwanted information, we can use a delegate function
to parse and extract only the file name.

This filter is very useful for me, so I think it could be useful for
other people :)

I currently don't have a gnome account, so I attach the file. Can
anybody inform me about requirements of the process to request one.

Thanks,

Mariano Cano

--
McP (mariano cano gmail com)
//
// FilterCompressedFiles.cs
//
// Copyright (C) 2007 Mariano Cano <mariano cano gmail com>
//

//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using Beagle.Daemon;
using Beagle.Util;

namespace Beagle.Filters {

  delegate string ExtractData(string line);
  
  public class FilterCompressedFiles : Beagle.Daemon.Filter {

    Hashtable commands;
    
    public FilterCompressedFiles ()
    {
      commands = new Hashtable();
      
      commands.Add("application/x-bzip-compressed-tar",
          new CompressedFilesHelper(new string[] {"tar","tjf"}));
      AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/x-bzip-compressed-tar"));

      commands.Add("application/x-compressed-tar",
          new CompressedFilesHelper(new string[] {"tar","tzf"}));
      AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/x-compressed-tar"));

      commands.Add("application/x-tar",
          new CompressedFilesHelper(new string[] {"tar","tf"}));
      AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/x-tar"));

      commands.Add("application/x-rar",
          new CompressedFilesHelper(new string[] {"unrar","lb"}));
      AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/x-rar"));
      
      commands.Add("application/zip",
          new CompressedFilesHelper(new string[] {"unzip","-l"}, new ExtractData(ParseUnzipLine)));
      AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/zip"));
    }

    public Hashtable Commands {
      get {return commands;}
    }

    override protected void DoPull ()
    {
      CompressedFilesHelper helper = (CompressedFilesHelper) Commands[MimeType];
      
      if (helper == null) {
        Finished();
        return;
      }

      string [] full_command = new string[helper.Command.Length + 1];
      helper.Command.CopyTo(full_command, 0);
      full_command[full_command.Length - 1] = FileInfo.FullName;
      
      SafeProcess pc = new SafeProcess ();
      pc.Arguments = full_command;
      pc.RedirectStandardOutput = true;
      
      try {
        pc.Start ();
      } catch (SafeProcessException e) {
        Log.Warn (e.Message);
        Error ();
        return;
      }
      
      StreamReader pout = new StreamReader (pc.StandardOutput);
      string str = null;

      while ((str = pout.ReadLine ()) != null) {
        AppendText (helper.ParseLine(str));
        AppendStructuralBreak ();
      }

      pout.Close ();
      pc.Close ();
      Finished();
    }

    string ParseUnzipLine(string line)
    {
      Match m = Regex.Match(line, @"^\s*\d+\s+[^\s]+\s+[^\s]+\s+(.+)$");
      
      if (!m.Success) {
        return "";
      }
      
      return (m.Groups[1].Value).Trim();
    }
  }

  class CompressedFilesHelper {
    string [] command;
    ExtractData extract_function = null;    
    
    public CompressedFilesHelper (string[] c, ExtractData f)
    {
      command = (string[]) c.Clone();
      extract_function = f;
    }

    public CompressedFilesHelper (string[] c) : this (c, null){}

    public string[] Command
    {
      get {return command;}
    }

    public string ParseLine(string line)
    {
      if (extract_function == null)
        return line;
      else
        return extract_function(line);
    }
  }
}


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