ifn='C:\Documents and Settings\Administrator\Desktop\crest.tar';
ofn='C:\Documents and Settings\Administrator\Desktop\crest3.tar';

data=[...
{'name',0,100};...
{'mode',0,8};...
{'user',0,8};...
{'group',0,8};...
{'size',0,12};...
{'modtime',0,12};...
{'chksum',0,8};...
{'link',0,1};...
{'lname',0,100};...
{'ustar',0,6};...
{'usver',0,2};...
{'uname',0,32};...
{'gname',0,32};...
{'maj',0,8};...
{'min',0,8};...
{'fnprefix',0,155};...
{'reserved',0,12};...
];

uu=num2cell(cumsum([0 data{1:end-1,3}]));
[data{:,2}]=deal(uu{:});

hd.offset=cell2struct(data(:,2)',data(:,1)',2);
hd.size=cell2struct(data(:,3)',data(:,1)',2);

header=struct('field',{data{:,1}},'offset',{data{:,2}},'size',{data{:,3}});
Nh=length(header);

linktype={'file','hlink','slink','cs','bs','dir','fifo','cfile'};

szfmt=sprintf('%%0%ds',hd.size.size-1);
chkfmt=sprintf('%%0%ds',hd.size.chksum-2);

finfo=dir(ifn);
NB=fix(finfo.bytes/512);
fid=fopen(ifn,'r');
ofid=fopen(ofn,'w');

nf=1;
while(~feof(fid))
  us=[];
  while(isempty(us) & ~feof(fid))
    block=fread(fid,512,'uchar');
    us=strfind(block','ustar');
    if(isempty(us) & ~feof(fid));
      fseek(fid,-4,'cof');
    end
  end
  if(~feof(fid))
    hp(nf)=ftell(fid)-512+us-hd.offset.ustar-1;
    if(nf>1)
      fseek(fid,hp(nf-1),'bof');
      block=fread(fid,512,'uchar');
      for nh=1:Nh
        head.(header(nh).field)=int8(block(header(nh).offset+(1:header(nh).size))');
      end
      asize=(hp(nf)-hp(nf-1)-512);
      bsize=ceil(asize/512)*512;
      head.size=[int8(sprintf(szfmt,dec2base(bsize,8))) 0];
      head.chksum=int8(32*ones(1,hd.size.chksum));
      u=struct2cell(head);
      head.chksum=[int8(sprintf(chkfmt,dec2base(sum([u{:}]),8))) 0 32];
      u=struct2cell(head);
      fwrite(ofid,[u{:}]);
      fwrite(ofid,fread(fid,asize,'uchar'));
      if(bsize-asize)
        fwrite(ofid,char(zeros(1,bsize-asize)),'uchar');
      end
      char(head.name)
      %pause;
    end
    fseek(fid,hp(nf)+512,'bof');
    nf=nf+1;
  else
    fseek(fid,hp(nf-1),'bof');
    block=fread(fid,512,'uchar');
    for nh=1:Nh
      head.(header(nh).field)=int8(block(header(nh).offset+(1:header(nh).size))');
    end
    asize=(finfo.bytes-hp(nf-1)-512);
    bsize=ceil(asize/512)*512;
    head.size=[int8(sprintf(szfmt,dec2base(bsize,8))) 0];
    head.chksum=int8(32*ones(1,hd.size.chksum));
    u=struct2cell(head);
    head.chksum=[int8(sprintf(chkfmt,dec2base(sum([u{:}]),8))) 0 32];
    u=struct2cell(head);
    fwrite(ofid,[u{:}]);
    fwrite(ofid,fread(fid,asize,'uchar'));
    if(bsize-asize)
      fwrite(ofid,char(zeros(1,bsize-asize)),'uchar');
    end
    block=fread(fid,1,'uchar');
    char(head.name)
  end
end
fclose(fid);
fclose(ofid);