Skip to content

Commit

Permalink
async_zip::base::read::memを使う
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Feb 14, 2024
1 parent ef5a344 commit 7b6d1b8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 74 deletions.
57 changes: 2 additions & 55 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/test_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "test_util"
edition.workspace = true

[dependencies]
async_zip = { workspace = true, features = ["full"] }
async_zip = { workspace = true, features = ["deflate"] }
futures-lite.workspace = true
once_cell.workspace = true
serde = { workspace = true, features = ["derive"] }
Expand Down
6 changes: 3 additions & 3 deletions crates/test_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ pub async fn convert_zip_vvm(dir: impl AsRef<Path>) -> PathBuf {
fs::create_dir_all(out_file_path.parent().unwrap())
.await
.unwrap();
let out_file = File::create(&out_file_path).await.unwrap();
let mut writer = ZipFileWriter::with_tokio(out_file);
let mut writer = ZipFileWriter::new(vec![]);

for entry in dir.read_dir().unwrap().flatten() {
let entry_builder = ZipEntryBuilder::new(
Expand All @@ -66,7 +65,8 @@ pub async fn convert_zip_vvm(dir: impl AsRef<Path>) -> PathBuf {
entry_writer.write_all(&buf).await.unwrap();
entry_writer.close().await.unwrap();
}
writer.close().await.unwrap();
let zip = writer.close().await.unwrap();
fs::write(&out_file_path, zip).await.unwrap();
}
out_file_path
}
2 changes: 1 addition & 1 deletion crates/voicevox_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ directml = ["onnxruntime/directml"]

[dependencies]
anyhow.workspace = true
async_zip = { workspace = true, features = ["full"] }
async_zip = { workspace = true, features = ["deflate"] }
camino.workspace = true
derive-getters.workspace = true
derive-new.workspace = true
Expand Down
32 changes: 18 additions & 14 deletions crates/voicevox_core/src/voice_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,24 @@ pub(crate) mod tokio {
}

#[derive(new)]
struct AsyncVvmEntryReader {
reader: async_zip::tokio::read::fs::ZipFileReader,
struct AsyncVvmEntryReader<'a> {
path: &'a Path,
reader: async_zip::base::read::mem::ZipFileReader,
entry_map: HashMap<String, AsyncVvmEntry>,
}

impl AsyncVvmEntryReader {
async fn open(path: &Path) -> LoadModelResult<Self> {
let reader = async_zip::tokio::read::fs::ZipFileReader::new(path)
.await
.map_err(|source| LoadModelError {
path: path.to_owned(),
context: LoadModelErrorKind::OpenZipFile,
source: Some(source.into()),
})?;
impl<'a> AsyncVvmEntryReader<'a> {
async fn open(path: &'a Path) -> LoadModelResult<Self> {
let reader = async {
let file = fs_err::tokio::read(path).await?;
async_zip::base::read::mem::ZipFileReader::new(file).await
}
.await
.map_err(|source| LoadModelError {
path: path.to_owned(),
context: LoadModelErrorKind::OpenZipFile,
source: Some(source.into()),
})?;
let entry_map: HashMap<_, _> = reader
.file()
.entries()
Expand All @@ -314,12 +318,12 @@ pub(crate) mod tokio {
.enumerate()
.map(|(i, (filename, entry))| (filename, AsyncVvmEntry { index: i, entry }))
.collect();
Ok(AsyncVvmEntryReader::new(reader, entry_map))
Ok(AsyncVvmEntryReader::new(path, reader, entry_map))
}
async fn read_vvm_json<T: DeserializeOwned>(&self, filename: &str) -> LoadModelResult<T> {
let bytes = self.read_vvm_entry(filename).await?;
serde_json::from_slice(&bytes).map_err(|source| LoadModelError {
path: self.reader.path().to_owned(),
path: self.path.to_owned(),
context: LoadModelErrorKind::ReadZipEntry {
filename: filename.to_owned(),
},
Expand All @@ -340,7 +344,7 @@ pub(crate) mod tokio {
}
.await
.map_err(|source| LoadModelError {
path: self.reader.path().to_owned(),
path: self.path.to_owned(),
context: LoadModelErrorKind::ReadZipEntry {
filename: filename.to_owned(),
},
Expand Down

0 comments on commit 7b6d1b8

Please sign in to comment.