Skip to content

Instantly share code, notes, and snippets.

@ndw
Created March 8, 2012 18:48
Show Gist options
  • Save ndw/2002633 to your computer and use it in GitHub Desktop.
Save ndw/2002633 to your computer and use it in GitHub Desktop.
Patch unique identifier in Linux Journal EPUB files
#!/usr/bin/perl -- # -*- Perl -*-
# Fixes the bookid identifier in Linux Journal EPUB files.
# No warranty. YMMV. etc.
use strict;
use English;
use Cwd qw(abs_path);
use File::Temp qw(tempdir);
my $ZIP = "zip";
my $UNZIP = "unzip";
my $JAVA = "java";
my $EPUBCHECK = "/usr/local/epubcheck-1.2/epubcheck-1.2.jar";
my $usage = "Usage: $0 dljxxx[.epub]\n";
my $EPUB = shift @ARGV || die $usage;
$EPUB = abs_path($EPUB);
$EPUB =~ s/\.epub$//;
my $ID = $EPUB;
$ID =~ s/^.*\///;
my $NPUB = "$EPUB-patched.epub";
my $tmp = tempdir('CLEANUP' => 1);
chdir ($tmp);
system ("$UNZIP -q $EPUB.epub");
my $content = "OEBPS/content.opf";
open (F, $content) || die "Unexpected archive format.";
read (F, $_, -s $content);
close (F);
system ("$ZIP -q0X $NPUB mimetype");
system ("$ZIP -qXr9D $NPUB *");
if (defined($EPUBCHECK)) {
system ("JAVA -jar $EPUBCHECK $NPUB");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment