Difference between revisions of "Contribute: Plugins/ExportPluginsZip"

From EPrints Documentation
Jump to: navigation, search
(Zip.pm: Initial code.)
(In More Detail: Initial code copy.)
Line 87: Line 87:
  
 
= In More Detail =
 
= In More Detail =
 +
<pre>
 +
package EPrints::Plugin::Export::MyPlugins::Zip;
 +
 +
@ISA = ("EPrints::Plugin::Export");
 +
 +
use strict;
 +
use Archive::Any::Create;
 +
use IO::File;
 +
use IO::String;
 +
 +
sub new
 +
{
 +
        my ($class, %opts) = @_;
 +
 +
        my $self = $class->SUPER::new(%opts);
 +
 +
        $self->{name} = "Zip";
 +
        $self->{accept} = [ 'list/eprint' ];
 +
        $self->{visible} = "all";
 +
        $self->{suffix} = ".zip";
 +
        $self->{mimetype} = "application/zip";
 +
 +
        return $self;
 +
}
 +
 +
sub output_list
 +
{
 +
my ($plugin, %opts) = @_;
 +
 +
my $archive = "";
 +
my $FH = IO::String->new(\$archive);
 +
my $zip = Archive::Any::Create->new;
 +
 +
foreach my $dataobj ($opts{"list"}->get_records)
 +
{
 +
my $dirpath = "eprints-search/".$dataobj->get_id()."/";
 +
 +
my $i = 1;
 +
foreach my $doc ($dataobj->get_all_documents)
 +
{
 +
my $subdirpath = $dirpath."doc$i/";
 +
my %files = $doc->files;
 +
 +
foreach my $filename (sort keys %files)
 +
{
 +
my $filepath = $subdirpath.$filename;
 +
my $file = $doc->local_path."/".$filename;
 +
 +
if (-d $file)
 +
{
 +
next;
 +
}
 +
 +
my $data = '';
 +
my $datafh = IO::String->new(\$data);
 +
 +
open (INFH, "<$file") or die ("Could not open file $file");
 +
while (<INFH>)
 +
{
 +
print {$datafh} $_;
 +
}
 +
close INFH;
 +
 +
$zip->add_file($filepath, $data);
 +
}
 +
$i++;
 +
}
 +
}
 +
 +
        if (defined $opts{"fh"})
 +
        {
 +
$zip->write_filehandle($opts{"fh"},"zip");
 +
                return undef;
 +
        }
 +
$zip->write_filehandle($FH,"zip");
 +
        return $archive;
 +
}
 +
 +
1;
 +
 +
</pre>
  
 
= Testing Your Plugin =
 
= Testing Your Plugin =
 
Restart your webserver and test the plugin as in [[User:Tom/Export_Plugins/Excel | the previous tutorial]].
 
Restart your webserver and test the plugin as in [[User:Tom/Export_Plugins/Excel | the previous tutorial]].

Revision as of 10:31, 30 August 2007

Export Plugin Tutorial 5: Zip

Zip.pm

package EPrints::Plugin::Export::MyPlugins::Zip;

@ISA = ("EPrints::Plugin::Export");

use strict;
use Archive::Any::Create;
use IO::File;
use IO::String;

sub new
{
        my ($class, %opts) = @_;

        my $self = $class->SUPER::new(%opts);

        $self->{name} = "Zip";
        $self->{accept} = [ 'list/eprint' ];
        $self->{visible} = "all";
        $self->{suffix} = ".zip";
        $self->{mimetype} = "application/zip";

        return $self;
}

sub output_list
{
	my ($plugin, %opts) = @_;

	my $archive = "";
	my $FH = IO::String->new(\$archive);
	my $zip = Archive::Any::Create->new;

	foreach my $dataobj ($opts{"list"}->get_records)
	{
		my $dirpath = "eprints-search/".$dataobj->get_id()."/";

		my $i = 1;
		foreach my $doc ($dataobj->get_all_documents)
		{
			my $subdirpath = $dirpath."doc$i/";
			my %files = $doc->files;

			foreach my $filename (sort keys %files)
			{
				my $filepath = $subdirpath.$filename;
				my $file = $doc->local_path."/".$filename;

				if (-d $file)
				{
					next;
				}

				my $data = '';
				my $datafh = IO::String->new(\$data);

				open (INFH, "<$file") or die ("Could not open file $file");
				while (<INFH>) 
				{
					print {$datafh} $_;
				}
				close INFH;
				
				$zip->add_file($filepath, $data);	
			}
			$i++;
		}
	}

        if (defined $opts{"fh"})
        {
		$zip->write_filehandle($opts{"fh"},"zip");
                return undef;
        }
	$zip->write_filehandle($FH,"zip");
        return $archive;
}

1;


In More Detail

package EPrints::Plugin::Export::MyPlugins::Zip;

@ISA = ("EPrints::Plugin::Export");

use strict;
use Archive::Any::Create;
use IO::File;
use IO::String;

sub new
{
        my ($class, %opts) = @_;

        my $self = $class->SUPER::new(%opts);

        $self->{name} = "Zip";
        $self->{accept} = [ 'list/eprint' ];
        $self->{visible} = "all";
        $self->{suffix} = ".zip";
        $self->{mimetype} = "application/zip";

        return $self;
}

sub output_list
{
	my ($plugin, %opts) = @_;

	my $archive = "";
	my $FH = IO::String->new(\$archive);
	my $zip = Archive::Any::Create->new;

	foreach my $dataobj ($opts{"list"}->get_records)
	{
		my $dirpath = "eprints-search/".$dataobj->get_id()."/";

		my $i = 1;
		foreach my $doc ($dataobj->get_all_documents)
		{
			my $subdirpath = $dirpath."doc$i/";
			my %files = $doc->files;

			foreach my $filename (sort keys %files)
			{
				my $filepath = $subdirpath.$filename;
				my $file = $doc->local_path."/".$filename;

				if (-d $file)
				{
					next;
				}

				my $data = '';
				my $datafh = IO::String->new(\$data);

				open (INFH, "<$file") or die ("Could not open file $file");
				while (<INFH>) 
				{
					print {$datafh} $_;
				}
				close INFH;
				
				$zip->add_file($filepath, $data);	
			}
			$i++;
		}
	}

        if (defined $opts{"fh"})
        {
		$zip->write_filehandle($opts{"fh"},"zip");
                return undef;
        }
	$zip->write_filehandle($FH,"zip");
        return $archive;
}

1;

Testing Your Plugin

Restart your webserver and test the plugin as in the previous tutorial.