-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
107 lines (87 loc) · 2.87 KB
/
lib.rs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::io::{self, BufReader, BufWriter, Read, Seek, Write};
use zip::ZipArchive;
/// Attempts to read the source as a ZIP archive, extract a file,
/// and output its contents to a provided writeable sink.
/// Both read and write operations are chunked
/// to minimize the number of system calls.
///
/// # Arguments
///
/// - `source` - Readable ZIP archive input.
/// - `filename` - Name of the file to be extracted.
/// - `buffer_size` - Size of the read and write buffers in bytes.
/// - `sink` - Writeable output for the extracted file bytes.
pub fn extract(
source: impl Read + Seek,
filename: &str,
buffer_size: usize,
sink: impl Write,
) -> io::Result<()> {
let mut archive = ZipArchive::new(source)?;
let file = archive.by_name(filename)?;
let reader = BufReader::with_capacity(buffer_size, file);
let mut writer = BufWriter::with_capacity(buffer_size, sink);
for byte in reader.bytes() {
writer.write_all(&[byte?])?;
}
writer.flush()
}
#[cfg(test)]
mod test {
use std::io::{self, Cursor, Read, Seek, Write};
use zip::{write::SimpleFileOptions, ZipWriter};
/// Creates a compressed ZIP archive in memory with the following files:
/// ```txt
/// .
/// ├── file.txt -> "outer"
/// └── nested
/// └── file.txt -> "inner"
/// ```
fn create_archive() -> io::Result<impl Read + Seek> {
let mut buffer = Cursor::new(Vec::new());
let mut zip = ZipWriter::new(&mut buffer);
let options =
SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
zip.start_file("file.txt", options)?;
zip.write_all(b"outer")?;
zip.add_directory("nested", options)?;
zip.start_file("nested/file.txt", options)?;
zip.write_all(b"inner")?;
zip.finish()?;
Ok(buffer)
}
#[test]
fn read_file() {
let source = create_archive().unwrap();
let mut sink = Vec::new();
super::extract(source, "file.txt", 8192, &mut sink).unwrap();
let content = String::from_utf8(sink).unwrap();
assert_eq!(content, "outer");
}
#[test]
fn read_file_in_dir() {
let source = create_archive().unwrap();
let mut sink = Vec::new();
super::extract(source, "nested/file.txt", 8192, &mut sink).unwrap();
let content = String::from_utf8(sink).unwrap();
assert_eq!(content, "inner");
}
#[test]
fn file_not_found() {
let source = create_archive().unwrap();
let mut sink = Vec::new();
let error = super::extract(source, "waldo", 8192, &mut sink).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::NotFound);
}
#[test]
fn buffer_overflow() {
let source = create_archive().unwrap();
let mut sink = [0u8; 3];
let error = super::extract(source, "file.txt", 8192, &mut sink[..]).unwrap_err();
let content = String::from_utf8(sink.to_vec()).unwrap();
// The buffer was too small for the file content
assert_eq!(error.kind(), io::ErrorKind::WriteZero);
// The buffer was only partially written
assert_eq!(content, "out");
}
}