Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How to concatenate binary files

by Tanktalus (Canon)
on Apr 12, 2005 at 21:38 UTC ( [id://447187]=note: print w/replies, xml ) Need Help??


in reply to How to concatenate binary files

Here's my sub on creating self-extracting EXE's which may come in useful. Note that this works on both Windows and Unix/Linux, even though it's always creating a Windows-based self-extracting EXE. (I know - I developed it on Linux, and just ran tests on Windows to make sure it still worked.)

use File::Copy; use IO::File; sub _create_sfx { my $self = shift; my $zip = shift; my $exe = shift; my $fh = IO::File->new($exe, "w"); binmode($fh); # stupid Windows copy(File::Spec->catfile(LocationOfUnzipsfxExe(), 'unzipsfx.exe'), $fh); copy($zip, $fh); unlink($zip); $fh->close(); system(qw(zip -A), $exe); }

As you can see, I open my output filehandle, set it to binary mode, and use File::Copy::copy to copy each filename (the unzipsfx.exe file and the zip file) into my still-open file handle. Once closed, then I tell zip to fix the sfx executable (offsets and stuff), and we're done. Funny thing is that "zip -A" run on Linux ... works to create the Windows self-extractor. :-)

Replies are listed 'Best First'.
Re^2: How to concatenate binary files
by tlm (Prior) on Apr 12, 2005 at 22:11 UTC

    I've never done this kind of thing, so this is a completely ignorant question: why don't you do the whole thing with a single call to zip -A, like this:

    my $sfx = File::Spec->catfile(LocationOfUnzipsfxExe(), 'unzipsfx.exe') +; system( qw( zip -A ), $exe, $sfx, $zip ) and die "$?"; unlink $zip;

    the lowliest monk

      That's a very good and interesting question. I suppose the reason why I did it the way above is because it's a perl interpretation of the unzipsfx docs (check the man page on unzipsfx - the EXAMPLES section near the bottom).

      A quick test shows:

      $ zip -A mysfx /usr/bin/unzipsfx x adding: usr/bin/unzipsfx (deflated 52%) adding: x (deflated 12%) $ file mysfx mysfx: Zip archive data, at least v2.0 to extract
      which is far from what we want. The output is still a zip file. But, if we do the shell version of the perl code above, we get:
      $ cat /usr/bin/unzipsfx mysfx > mysfx_exe $ zip -A mysfx_exe mysfx_exe: adjusting offsets for a preamble of 47552 bytes $ file mysfx_exe mysfx_exe: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), f +or GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped
      I hope that helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://447187]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-25 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found