use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use flate2::read::DeflateDecoder;
use zip::CompressionMethod;
use zip::read::ZipArchive;
use crate::error::{ExcelError, Result};
pub trait ReadSeek: Read + Seek {}
impl<T: Read + Seek> ReadSeek for T {}
pub type BoxedReader = Box<dyn ReadSeek + Send>;
#[derive(Clone)]
pub enum ZipSource {
Path(PathBuf),
Bytes(Arc<Vec<u8>>),
}
impl std::fmt::Debug for ZipSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ZipSource::Path(p) => write!(f, "ZipSource::Path({})", p.display()),
ZipSource::Bytes(b) => write!(f, "ZipSource::Bytes({} bytes)", b.len()),
}
}
}
impl ZipSource {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let path = path.as_ref().to_path_buf();
if !path.exists() {
return Err(ExcelError::Io(format!("文件不存在:{}", path.display())));
}
Ok(ZipSource::Path(path))
}
pub fn from_bytes(bytes: Vec<u8>) -> Self {
ZipSource::Bytes(Arc::new(bytes))
}
pub fn from_arc(bytes: Arc<Vec<u8>>) -> Self {
ZipSource::Bytes(bytes)
}
pub fn path(&self) -> Option<&Path> {
match self {
ZipSource::Path(p) => Some(p.as_path()),
ZipSource::Bytes(_) => None,
}
}
pub fn len(&self) -> u64 {
match self {
ZipSource::Path(p) => std::fs::metadata(p).map(|m| m.len()).unwrap_or(0),
ZipSource::Bytes(b) => b.len() as u64,
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
fn open_raw(&self) -> Result<BoxedReader> {
match self {
ZipSource::Path(p) => Ok(Box::new(BufReader::with_capacity(
128 * 1024,
File::open(p)?,
))),
ZipSource::Bytes(b) => Ok(Box::new(SharedBytes::new(b.clone()))),
}
}
pub fn open_archive(&self) -> Result<ZipArchive<BoxedReader>> {
ZipArchive::new(self.open_raw()?).map_err(|e| ExcelError::NotXlsx(e.to_string()))
}
pub fn read_entry(&self, name: &str) -> Result<Vec<u8>> {
let meta = self.entry_meta(name)?;
let mut reader = self.open_entry_reader(&meta)?;
let mut buf = Vec::with_capacity(meta.uncompressed_size.min(64 * 1024 * 1024) as usize);
reader.read_to_end(&mut buf)?;
Ok(buf)
}
pub fn has_entry(&self, name: &str) -> bool {
match self.open_archive() {
Ok(mut a) => a.by_name(name).is_ok(),
Err(_) => false,
}
}
pub fn entry_names(&self) -> Result<Vec<String>> {
let archive = self.open_archive()?;
Ok(archive.file_names().map(|s| s.to_string()).collect())
}
pub fn entry_meta(&self, name: &str) -> Result<EntryMeta> {
let mut archive = self.open_archive()?;
let file = archive
.by_name(name)
.map_err(|_| ExcelError::NotXlsx(format!("缺少内部条目:{name}")))?;
if file.encrypted() {
return Err(ExcelError::Unsupported(
"加密的 xlsx 文件暂不支持".to_string(),
));
}
Ok(EntryMeta {
name: name.to_string(),
data_start: file.data_start(),
compression: file.compression(),
compressed_size: file.compressed_size(),
uncompressed_size: file.size(),
})
}
pub fn open_entry_reader(&self, meta: &EntryMeta) -> Result<EntryReader> {
let mut raw = self.open_raw()?;
raw.seek(SeekFrom::Start(meta.data_start))?;
let limited = raw.take(meta.compressed_size);
let inner: Box<dyn Read + Send> = match meta.compression {
CompressionMethod::Stored => Box::new(limited),
CompressionMethod::Deflated => Box::new(DeflateDecoder::new(limited)),
other => {
return Err(ExcelError::Unsupported(format!(
"条目不支持的压缩方式 {other:?}({})",
meta.name
)));
}
};
Ok(EntryReader {
inner,
remaining: meta.uncompressed_size,
})
}
pub fn entry_reader(&self, name: &str) -> Result<EntryReader> {
let meta = self.entry_meta(name)?;
self.open_entry_reader(&meta)
}
}
#[derive(Debug, Clone)]
pub struct EntryMeta {
pub name: String,
pub data_start: u64,
pub compression: CompressionMethod,
pub compressed_size: u64,
pub uncompressed_size: u64,
}
pub struct EntryReader {
inner: Box<dyn Read + Send>,
remaining: u64,
}
impl EntryReader {
pub fn uncompressed_size(&self) -> u64 {
self.remaining
}
}
impl Read for EntryReader {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.inner.read(buf)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_missing_file() {
let err = ZipSource::open("definitely-not-exists.xlsx").unwrap_err();
assert!(matches!(err, ExcelError::Io(_)));
}
}
pub(crate) struct SharedBytes {
data: Arc<Vec<u8>>,
pos: u64,
}
impl SharedBytes {
pub(crate) fn new(data: Arc<Vec<u8>>) -> Self {
Self { data, pos: 0 }
}
}
impl Read for SharedBytes {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let data: &[u8] = &self.data;
let start = self.pos as usize;
if start >= data.len() {
return Ok(0);
}
let n = (data.len() - start).min(buf.len());
buf[..n].copy_from_slice(&data[start..start + n]);
self.pos += n as u64;
Ok(n)
}
}
impl Seek for SharedBytes {
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
let len = self.data.len() as i64;
let next = match pos {
SeekFrom::Start(n) => n as i64,
SeekFrom::End(n) => len + n,
SeekFrom::Current(n) => self.pos as i64 + n,
};
if next < 0 {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"seek 到负数位置",
));
}
self.pos = next as u64;
Ok(self.pos)
}
}
#[cfg(test)]
mod shared_bytes_tests {
use super::*;
#[test]
fn test_shared_bytes_seek_read() {
let data = Arc::new(vec![1u8, 2, 3, 4, 5]);
let mut r = SharedBytes::new(data);
assert_eq!(r.seek(SeekFrom::Start(2)).unwrap(), 2);
let mut buf = [0u8; 2];
r.read_exact(&mut buf).unwrap();
assert_eq!(buf, [3, 4]);
assert_eq!(r.seek(SeekFrom::End(-1)).unwrap(), 4);
assert_eq!(r.read(&mut buf).unwrap(), 1);
assert_eq!(buf[0], 5);
}
}