Skip to content

Commit

Permalink
Delete excess zip files
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerchin committed Mar 21, 2024
1 parent 9955d27 commit 07427d8
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/maple/gtfs_handlers/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ use std::fs::File;
use std::io::Cursor;
use std::io::Read;
use std::path::PathBuf;
use std::fs::{read_dir, remove_file};

fn delete_zip_files(dir_path: &str) -> std::io::Result<()> {
for entry in read_dir(dir_path)? {
let entry = entry?;
let path = entry.path();
if path.is_file() && path.extension().map_or(false, |ext| ext == "zip") {
// don't crash if you can't delete the zips
let _ = remove_file(path);
}
}
Ok(())
}

// Extracts a sub zip file and uses it as the parent folder
pub fn extract_sub_zip(feed_id: &str, sub_folder: &str) -> Result<(), Box<dyn Error>> {
Expand All @@ -16,10 +29,13 @@ pub fn extract_sub_zip(feed_id: &str, sub_folder: &str) -> Result<(), Box<dyn Er

// read bytes and pass back error if unable to read
let read = file.read_to_end(&mut buf)?;
let target_dir = PathBuf::from(target_path);
let target_dir = PathBuf::from(target_path.as_str());

zip_extract::extract(Cursor::new(buf), &target_dir, true)?;

//delete excess zip files
delete_zip_files(target_path.as_str())?;

Ok(())
}

Expand Down

0 comments on commit 07427d8

Please sign in to comment.