class Crystar::Writer

Overview

Writer provides sequential writing of a tar archive. Writer#write_header begins a new file with the provided Header, and then Writer can be treated as an io.Writer to supply that file's data via invoking Writer#write method .

Example

require "tar"

File.open("./file.tar", "w") do |file|
  Crystar::Writer.open(file) do |tar|
    # add file to archive
    tar.add File.open("./some_file.txt")
    # Manually create the Header with info per your choice
    hdr = Header.new(
      name: "Your file Name",
      size: 100_i64,  # Contents size
      mode: 0o644_i64 # Permission and mode bits
    # ..... Look into `Crystar::Header`
    )
    tar.write_header hdr
    tar.write "your file contents".to_slice

    # Create header from File you have already opened.
    hdr = file_info_header(file, file.path)
    tar.write_header hdr
    tar.write file.gets_to_end.to_slice
  end
end

Defined in:

tar/writer.cr

Constant Summary

LONG_NAME = "././@LongLink"

Use long-link files if Name or Linkname exceeds the field size.

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(io : IO, sync_close = false) #

Creates a new writer to the given io.


[View source]
def self.new(filename : String) #

Creates a new writer to the given filename.


[View source]

Class Method Detail

def self.open(io : IO, sync_close = false, &block) #

Creates a new writer to the given io, yields it to the given block, and closes it at the end.


[View source]
def self.open(filename : String, &block) #

Creates a new writer to the given filename, yields it to the given block, and closes it at the end.


[View source]

Instance Method Detail

def add(file : File) #

Adds an entry that will have its data copied from the given file. file is automatically closed after data is copied from it.


[View source]
def close #

Close closes the tar archive by flushing the padding, and writing the footer. If the current file (from a prior call to WriteHeader) is not fully written, then this returns an error.


[View source]
def closed? : Bool #

Returns true if this writer is closed.


[View source]
def hdr=(hdr : Header) #

Copy of Header that is safe for mutations


[View source]
def sync_close=(sync_close) #

Whether to close the enclosed IO when closing this writer.


[View source]
def sync_close? : Bool #

Whether to close the enclosed IO when closing this writer.


[View source]
def write(b : Bytes) #

Write writes to the current file in the tar archive. Write returns the error ErrWriteTooLong if more than Header.Size bytes are written after WriteHeader.

Calling Write on special types like LINK, SYMLINK, CHAR, BLOCK, DIR, and FIFO returns (0, ErrWriteTooLong) regardless of what the Header.Size claims.


[View source]
def write_header(hdr : Header) : Nil #

write_header writes hdr and prepares to accept the file's contents. The Header.Size determines how many bytes can be written for the next file. If the current file is not fully written, then this returns an error. This implicitly flushes any padding necessary before writing the header.


[View source]