1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
pub mod archive;
pub mod error;
pub mod file;
pub mod source;

use error::Error;
use error::Result;

use axfive_libzip_sys as ffi;

#[cfg(test)]
mod tests {
    use super::*;
    use std::convert::TryInto;
    use std::ffi::{CStr, CString};
    use std::io::Read;
    use std::os::raw::{c_int, c_void};
    use std::string::String;
    use std::vec::Vec;
    use tempdir::TempDir;

    #[test]
    fn round_trip() {
        let tempdir = TempDir::new("test").unwrap();
        let zip_path = CString::new(tempdir.path().join("file.zip").to_str().unwrap()).unwrap();
        let foo = "Lorem ipsum dolor sit amet";
        let bar = "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";

        {
            let file_source: source::Source<source::File> =
                (&zip_path as &CStr).try_into().unwrap();
            let mut archive = archive::Archive::open(
                file_source,
                &[archive::OpenFlag::Create, archive::OpenFlag::Exclusive],
            )
            .unwrap();
            let foo_source: source::Source<&[u8]> = foo.as_bytes().try_into().unwrap();
            archive
                .add(
                    &CString::new("foo").unwrap(),
                    foo_source,
                    file::Encoding::Guess,
                    false,
                )
                .unwrap();
            let bar_source: source::Source<&[u8]> = bar.as_bytes().try_into().unwrap();
            archive
                .add(
                    &CString::new("bar").unwrap(),
                    bar_source,
                    file::Encoding::Guess,
                    false,
                )
                .unwrap();
            archive.close().unwrap();
        }

        {
            let file_source: source::Source<source::File> =
                (&zip_path as &CStr).try_into().unwrap();
            let mut archive = archive::Archive::open(
                file_source,
                &[
                    archive::OpenFlag::CheckConsistency,
                    archive::OpenFlag::ReadOnly,
                ],
            )
            .unwrap();
            let mut foo_buf = String::new();
            archive
                .open_file(&CString::new("foo").unwrap(), &[], &[])
                .unwrap()
                .read_to_string(&mut foo_buf)
                .unwrap();
            assert_eq!(foo_buf, foo);
            let mut bar_buf = String::new();
            archive
                .open_file(&CString::new("bar").unwrap(), &[], &[])
                .unwrap()
                .read_to_string(&mut bar_buf)
                .unwrap();
            assert_eq!(bar_buf, bar);
            archive.close().unwrap();
        }
    }
}