vfs_zip/write/
_write.rs

1#[cfg(feature = "vfs04")] mod vfs04;
2
3use crate::Result;
4
5use std::collections::*;
6use std::fmt::{self, Debug, Formatter};
7use std::io::{Write, Seek};
8use std::sync::{Arc, Mutex};
9
10
11
12/// A write-only zip archive filesystem
13pub struct ZipWriteOnly<IO: Write + Seek + Send + 'static> {
14    imp:    Arc<Mutex<Imp<IO>>>,
15    weak:   bool,
16}
17
18struct Imp<IO: Write + Seek + Send + 'static> {
19    writer: zip::write::ZipWriter<IO>,
20    dirs:   BTreeSet<String>,
21}
22
23impl<IO: Write + Seek + Send> Debug for ZipWriteOnly<IO> {
24    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
25        write!(fmt, "ZipWriteOnly")
26    }
27}
28
29impl<IO: Write + Seek + Send> ZipWriteOnly<IO> {
30    /// Create a new write-only zip filesystem.
31    ///
32    /// The underlying I/O will not be closed until the filesystem and all outstanding files are dropped.
33    pub fn new_strong(io: IO) -> Result<Self> { Self::new_impl(io, false) }
34
35    /// Create a new write-only zip filesystem.
36    ///
37    /// The underlying I/O will be closed when the filesystem is dropped.
38    /// Any outstanding files will start generating I/O errors and will not be committed to the .zip
39    pub fn new_weak(io: IO) -> Result<Self> { Self::new_impl(io, true) }
40
41    fn new_impl(io: IO, weak: bool) -> Result<Self> {
42        Ok(Self {
43            imp: Arc::new(Mutex::new(Imp {
44                writer: zip::write::ZipWriter::new(io),
45                dirs:   Default::default(),
46            })),
47            weak,
48        })
49    }
50}