teodozjan
teodozjan

Reputation: 913

Why rewriting binary file does not work?

I have almost tar.gz file. I need to skip some bytes before unpacking and then untar it, change something and pack it again.

 #!perl

 open (my $src, "<", $path) or die $!;
 binmode($src);
 seek($src, $magicNumber,0);

 open(my $tgz, ">", $path . ".tar.gz");
 binmode($tgz);

 #while(<$src>){
 #    print $tgz $_;
 #}
 my $n; my $data;
 while(($n = read $src, $data, 4) != 0){
     print $tgz $data;
 }

Why when using uncommented version generetes shorter file than uncomented (byte skipped in the middle). I would understand if end of file would be corrupted but generated files differ somewhere in the middle. Any explanation for this?

Unfortunately I am not allowed to upload file that reproduces problem.

Upvotes: 1

Views: 106

Answers (1)

ElPaco
ElPaco

Reputation: 77

Using constructs like foreach(<$src>) for binary files is generally not very good since it reads in line mode (and binary files does not have the concept of lines).

If you do a binary compare between your files I would guess that the bytes you are missing is either a CR or a LF.

Upvotes: 4

Related Questions