axfive_libzip/
lib.rs

1pub mod archive;
2pub mod error;
3pub mod file;
4pub mod source;
5
6use error::Error;
7use error::Result;
8use axfive_libzip_sys as ffi;
9
10#[cfg(test)]
11mod tests {
12    use super::*;
13    use std::convert::TryInto;
14    use std::ffi::{CStr, CString};
15    use std::io::Read;
16
17    use std::string::String;
18
19    use tempdir::TempDir;
20
21    #[test]
22    fn round_trip() {
23        let tempdir = TempDir::new("test").unwrap();
24        let zip_path = CString::new(tempdir.path().join("file.zip").to_str().unwrap()).unwrap();
25        let foo = "Lorem ipsum dolor sit amet";
26        let bar = "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
27        let baz = "Ut enim ad minim veniam, quis nostrud exercitation";
28
29        {
30            let file_source: source::Source =
31                (&zip_path as &CStr).try_into().unwrap();
32            let mut archive = archive::Archive::open(
33                file_source,
34                [archive::OpenFlag::Create, archive::OpenFlag::Exclusive],
35            )
36            .unwrap();
37            let foo_source: source::Source = foo.as_bytes().try_into().unwrap();
38            archive
39                .add(
40                    CString::new("foo").unwrap(),
41                    foo_source,
42                    file::Encoding::Guess,
43                    file::Compression::Default,
44                    None,
45                    None,
46                    false,
47                )
48                .unwrap();
49            let bar_source: source::Source = bar.as_bytes().try_into().unwrap();
50            archive
51                .add(
52                    CString::new("bar").unwrap(),
53                    bar_source,
54                    file::Encoding::Guess,
55                    file::Compression::Default,
56                    None,
57                    None,
58                    false,
59                )
60                .unwrap();
61            let baz_source: source::Source = (Box::new(baz.as_bytes()) as Box<dyn std::io::Read>)
62                .try_into()
63                .unwrap();
64            archive
65                .add(
66                    CString::new("baz").unwrap(),
67                    baz_source,
68                    file::Encoding::Guess,
69                    file::Compression::Default,
70                    None,
71                    None,
72                    false,
73                )
74                .unwrap();
75            archive.close().unwrap();
76        }
77
78        {
79            let file_source: source::Source =
80                (&zip_path as &CStr).try_into().unwrap();
81            let mut archive = archive::Archive::open(
82                file_source,
83                [
84                    archive::OpenFlag::CheckConsistency,
85                    archive::OpenFlag::ReadOnly,
86                ],
87            )
88            .unwrap();
89            let mut foo_buf = String::new();
90            archive
91                .open_file(CString::new("foo").unwrap(), [], [])
92                .unwrap()
93                .read_to_string(&mut foo_buf)
94                .unwrap();
95            assert_eq!(foo_buf, foo);
96            let mut bar_buf = String::new();
97            archive
98                .open_file(CString::new("bar").unwrap(), [], [])
99                .unwrap()
100                .read_to_string(&mut bar_buf)
101                .unwrap();
102            assert_eq!(bar_buf, bar);
103            let mut baz_buf = String::new();
104            archive
105                .open_file(CString::new("baz").unwrap(), [], [])
106                .unwrap()
107                .read_to_string(&mut baz_buf)
108                .unwrap();
109            assert_eq!(baz_buf, baz);
110            archive.close().unwrap();
111        }
112    }
113}