class Crystar::Writer
- Crystar::Writer
- Reference
- Object
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.crConstant Summary
-
LONG_NAME =
"././@LongLink"
-
Use long-link files if Name or Linkname exceeds the field size.
Constructors
-
.new(io : IO, sync_close = false)
Creates a new writer to the given io.
-
.new(filename : String)
Creates a new writer to the given filename.
Class Method Summary
-
.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.
-
.open(filename : String, &block)
Creates a new writer to the given filename, yields it to the given block, and closes it at the end.
Instance Method Summary
-
#add(file : File)
Adds an entry that will have its data copied from the given file.
-
#close
Close closes the tar archive by flushing the padding, and writing the footer.
-
#closed? : Bool
Returns
true
if this writer is closed. -
#hdr=(hdr : Header)
Copy of Header that is safe for mutations
-
#sync_close=(sync_close)
Whether to close the enclosed
IO
when closing this writer. -
#sync_close? : Bool
Whether to close the enclosed
IO
when closing this writer. -
#write(b : Bytes)
Write writes to the current file in the tar archive.
-
#write_header(hdr : Header) : Nil
write_header writes hdr and prepares to accept the file's contents.
Constructor Detail
Class Method Detail
Creates a new writer to the given io, yields it to the given block, and closes it at the end.
Creates a new writer to the given filename, yields it to the given block, and closes it at the end.
Instance Method Detail
Adds an entry that will have its data copied from the given file. file is automatically closed after data is copied from it.
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.
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.
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.