1use crate::{
4 CompressionMethod, Timestamp,
5 compression::Compressor,
6 io,
7 utils::{Counter, Crc32Writer},
8};
9use alloc::{fmt, vec::Vec};
10
11mod raw;
12
13#[derive(Debug, Default, Clone)]
17#[non_exhaustive]
18pub struct FileOptions {
19 pub compression_method: CompressionMethod,
21 pub level: Option<i32>,
23 pub modified_at: Timestamp,
28 pub unix_mode: u32,
31}
32
33impl FileOptions {
34 pub fn compression(mut self, method: CompressionMethod, level: Option<i32>) -> Self {
36 self.compression_method = method;
37 self.level = level;
38 self
39 }
40
41 pub fn modified_at(mut self, modified_at: Timestamp) -> Self {
43 self.modified_at = modified_at;
44 self
45 }
46
47 pub fn unix_mode(mut self, unix_mode: u32) -> Self {
49 self.unix_mode = unix_mode;
50 self
51 }
52}
53
54#[derive(Debug, Default)]
87pub struct ArchiveWriter<W: io::Write> {
88 writer: W,
89 raw: raw::RawArchiveWriter,
90}
91
92#[cfg(feature = "std")]
93impl ArchiveWriter<std::fs::File> {
94 pub fn create(path: impl AsRef<std::path::Path>) -> io::Result<Self> {
99 std::fs::File::create(path).map(Self::new)
100 }
101
102 pub fn create_new(path: impl AsRef<std::path::Path>) -> io::Result<Self> {
105 std::fs::File::create_new(path).map(Self::new)
106 }
107}
108
109impl<W: io::Write> ArchiveWriter<W> {
110 #[inline]
112 pub fn new(writer: W) -> Self {
113 ArchiveWriter {
114 writer,
115 raw: raw::RawArchiveWriter::default(),
116 }
117 }
118
119 pub fn add_file<R: io::Read>(
123 &mut self,
124 name: &str,
125 mut content: R,
126 options: &FileOptions,
127 ) -> io::Result<()> {
128 let mut w = Crc32Writer::new(Compressor::new(
129 Vec::new(),
130 options.compression_method,
131 options.level,
132 )?);
133 let uncompressed_size = io::copy(&mut content, &mut w)?;
134 let crc32 = w.result();
135 let compressed = w.into_inner().finish()?;
136
137 self.raw.write_file_raw(
138 &mut self.writer,
139 name,
140 &compressed,
141 &raw::Metadata {
142 compression_method: options.compression_method,
143 compressed_size: compressed.len() as _,
144 uncompressed_size,
145 crc32,
146 typ: crate::FileType::File,
147 modified_at: options.modified_at,
148 unix_mode: options.unix_mode,
149 },
150 "",
151 )?;
152
153 Ok(())
154 }
155
156 pub fn stream_file(
163 &mut self,
164 name: &str,
165 options: &FileOptions,
166 ) -> io::Result<FileStreamer<'_, W>> {
167 let writer = self.raw.start_stream_raw(&mut self.writer, name, options)?;
168
169 Ok(FileStreamer {
170 writer: Counter::new(Crc32Writer::new(Compressor::new(
171 writer,
172 options.compression_method,
173 options.level,
174 )?)),
175 })
176 }
177
178 pub fn add_directory(&mut self, name: &str) -> io::Result<()> {
180 self.raw.write_file_raw(
181 &mut self.writer,
182 name,
183 &[],
184 &raw::Metadata {
185 compression_method: CompressionMethod::STORE,
186 compressed_size: 0,
187 uncompressed_size: 0,
188 crc32: 0,
189 typ: crate::FileType::Directory,
190 modified_at: Timestamp::UNIX_EPOCH,
191 unix_mode: 0,
192 },
193 "",
194 )
195 }
196
197 pub fn add_symlink(&mut self, name: &str, target: &str) -> io::Result<()> {
203 self.raw.write_file_raw(
204 &mut self.writer,
205 name,
206 target.as_bytes(),
207 &raw::Metadata {
208 compression_method: CompressionMethod::STORE,
209 compressed_size: target.len() as _,
210 uncompressed_size: target.len() as _,
211 crc32: crc32fast::hash(target.as_bytes()),
212 typ: crate::FileType::Symlink,
213 modified_at: Timestamp::UNIX_EPOCH,
214 unix_mode: 0o777,
215 },
216 "",
217 )
218 }
219
220 #[inline]
228 pub fn recover(&mut self) -> io::Result<()>
229 where
230 W: io::Seek,
231 {
232 self.raw.recover(&mut self.writer)
233 }
234
235 #[inline]
237 pub fn get_ref(&self) -> &W {
238 &self.writer
239 }
240
241 #[inline]
245 pub fn get_mut(&mut self) -> &mut W {
246 &mut self.writer
247 }
248
249 #[inline]
251 pub fn flush(&mut self) -> io::Result<()> {
252 self.writer.flush()
253 }
254
255 #[inline]
260 pub fn finish(mut self) -> io::Result<W> {
261 self.raw.finish(&mut self.writer)?;
262 Ok(self.writer)
263 }
264}
265
266pub struct FileStreamer<'a, W: io::Write> {
272 writer: Counter<Crc32Writer<Compressor<raw::RawFileStreamer<'a, &'a mut W>>>>,
273}
274
275impl<W: io::Write> io::Write for FileStreamer<'_, W> {
276 #[inline]
277 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
278 self.writer.write(buf)
279 }
280
281 #[inline]
282 fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
283 self.writer.write_vectored(bufs)
284 }
285
286 #[inline]
287 fn flush(&mut self) -> io::Result<()> {
288 self.writer.flush()
289 }
290}
291
292impl<W: io::Write> FileStreamer<'_, W> {
293 pub fn finish(self) -> io::Result<()> {
295 self.finish_with_comment("")
296 }
297
298 pub fn finish_with_comment(self, comment: &str) -> io::Result<()> {
300 let uncompressed_size = self.writer.amt;
301 let crc32 = self.writer.inner.result();
302
303 let raw_writer = self.writer.inner.into_inner().finish()?;
304
305 raw_writer.finish(uncompressed_size, crc32, comment)
306 }
307}
308
309impl<'a, W: io::Write + fmt::Debug> fmt::Debug for FileStreamer<'a, W> {
310 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
311 f.write_str("FileStreamer { .. }")
312 }
313}