Skip to content

Instantly share code, notes, and snippets.

@psychon
Created January 21, 2023 09:55
Show Gist options
  • Save psychon/01bbe2bb6d09ddcb8ea7c7e36d2c0bd5 to your computer and use it in GitHub Desktop.
Save psychon/01bbe2bb6d09ddcb8ea7c7e36d2c0bd5 to your computer and use it in GitHub Desktop.
Sample program to download a ZIP file and show its content
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "0.11", features = ["blocking"] }
vfs-zip = "0.2.1"
vfs = "0.4"
use std::io::Read as _;
use vfs::filesystem::FileSystem as _;
const URL: &str =
"https://drive.google.com/uc?export=download&id=1yAmFc15GtP52El_RTxl6uqmZZJi-h4BG";
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut data = Vec::new();
let mut response = reqwest::blocking::get(URL)?;
response.copy_to(&mut data)?;
let cursor = std::io::Cursor::new(data);
let zip = vfs_zip::ZipReadOnly::new_strict(cursor)?;
for entry in zip.read_dir("/")? {
let metadata = zip.metadata(&entry)?;
println!("{entry}: {metadata:?}");
if metadata.file_type == vfs::path::VfsFileType::File {
let data = zip
.open_file(&entry)?
.bytes()
.take(10)
.collect::<Result<Vec<_>, _>>()?;
println!("First few bytes of file data: {data:?}");
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment