;Unpack file(s) from a tar file.
;
; Inputs
;   pack_filename - Filename of tar file. Must be string scalar
;   filename      - File to extract, including path inside of tar if necessary. May be vector
;
; Keyword inputs
;   output_dir    - Path to write file into
;   /verbose      - Print archive manager command line and output
pro unpack_file,pack_filename,filename,verbose=verbose,output_dir=output_dir
  if n_elements(output_dir) eq 0 then output_dir=!SDS_ROOT

  if (!VERSION.OS_FAMILY eq 'Windows') then begin
     spawn_cmd = !7Z_PATH+'/7za x -aoa -o"'+output_dir+ '" "'+pack_filename+'" "'+filename+'"'
     if keyword_set(verbose) then print,spawn_cmd
     spawn,spawn_cmd,/noshell,result
  endif else begin
     list = pack_list(pack_filename,verbose=verbose)
     UP_list = STRUPCASE(list)
     UP_filename = STRUPCASE(filename)
     index = where(UP_filename eq UP_list)
     if(index lt 0) then return
     tar_cmd = 'tar x -C "'+output_dir+'" -f "' + pack_filename + '" "'+ list[index] +'"'
     if keyword_set(verbose) then print,tar_cmd
     spawn, tar_cmd, result
     if (list[index] ne filename) then $
        spawn, 'mv "'+output_dir+'/'+list[index]+'" "'+output_dir+'/'+filename+'"', mv_result
  endelse

  ; show output
  if keyword_set(verbose) then for i=0,n_elements(result)-1 do print,result[i]

end
