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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::{
super::{context::*, url::*, util::*},
zip_url::*,
};
impl URL for ZipUrl {
fn context(&self) -> &UrlContext {
&*self.context
}
fn query(&self) -> Option<UrlQuery> {
self.archive_url.query()
}
fn fragment(&self) -> Option<String> {
self.archive_url.fragment()
}
fn format(&self) -> Option<String> {
get_format_from_path(&self.path)
}
fn base(&self) -> Option<UrlRef> {
get_relative_path_parent(&self.path).map(|path| self.new_with(path).into())
}
fn relative(&self, path: &str) -> UrlRef {
self.new_with(self.path.join(path)).into()
}
#[cfg(feature = "blocking")]
fn conform(&mut self) -> Result<(), super::super::UrlError> {
// (We assume the archive URL has already been conformed)
// Note that ZIP entries could have relative or absolute paths
// (though absolute paths are rare), so we cannot conform to absolute
self.path = self.path.normalize();
self.open()?;
Ok(())
}
#[cfg(feature = "async")]
fn conform_async(&self) -> Result<ConformFuture, super::super::UrlError> {
use super::super::errors::*;
async fn conform_async(mut url: ZipUrl) -> Result<UrlRef, UrlError> {
// (We assume the archive URL has already been conformed)
// Note that ZIP entries could have relative or absolute paths
// (though absolute paths are rare), so we cannot conform to absolute
url.path = url.path.normalize();
let _ = url.open_async()?;
Ok(url.into())
}
Ok(Box::pin(conform_async(self.clone())))
}
#[cfg(feature = "blocking")]
fn open(&self) -> Result<ReadRef, super::super::UrlError> {
use {
super::blocking::*,
kutil::std::error::*,
std::{fs::*, sync::*},
};
let archive_path = match self.archive_url.local() {
Some(path) => Mutex::new(path).into(),
None => {
let (path, _) = self.context.cache.file_from(&self.archive_url, "zip-")?;
path
}
};
let archive_path = archive_path.lock()?;
let file = File::open(archive_path.clone()).with_path(archive_path.clone())?;
let archive = file.read_zip_move()?;
let entry = archive.by_name(self)?;
Ok(Box::new(entry.reader()?))
// Read all:
//
// let archive = file.read_zip()?;
// if let Some(entry) = archive.by_name(&self.path) {
// // We can't detatch the reader, so must read all the bytes here :(
// // let bytes = entry.bytes()?;
// // return Ok(Box::new(Cursor::new(bytes)));
// }
// Err(UrlError::new_io_not_found(self))
}
#[cfg(feature = "async")]
fn open_async(&self) -> Result<OpenFuture, super::super::UrlError> {
use {
super::{super::errors::*, asynchronous::*},
kutil::std::error::*,
positioned_io::*,
std::sync::*,
};
async fn open_async(url: ZipUrl) -> Result<AsyncReadRef, UrlError> {
let archive_path = match url.archive_url.local() {
Some(path) => Mutex::new(path).into(),
None => {
let (path, _) = url.context.cache.file_from_async(&url.archive_url, "zip-").await?;
path
}
};
let archive_path = archive_path.lock()?;
let file = Arc::new(RandomAccessFile::open(archive_path.clone()).with_path(archive_path.clone())?);
let archive = file.read_zip_move().await?;
let entry = archive.by_name(&url).await?;
Ok(Box::pin(entry.reader()?))
// Read all:
//
// let archive = file.read_zip().await?;
// if let Some(entry) = archive.by_name(&url.path) {
// // We can't detatch the reader, so must read all the bytes here :(
// let bytes = entry.bytes().await?;
// return Ok(Box::pin(Cursor::new(bytes)));
// }
// Err(UrlError::new_io_not_found(url))
}
Ok(Box::pin(open_async(self.clone())))
}
}