Skip to content

Commit

Permalink
[rust] Add XZ uncompressor
Browse files Browse the repository at this point in the history
Add a XZ uncompressor to uncompress the new Firefox nightly builds for
Linux. This is based on the BZ2 uncompressor.
  • Loading branch information
bgermann committed Dec 3, 2024
1 parent 644147f commit ffb2a9e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rust/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use apple_flat_package::PkgReader;
use bzip2::read::BzDecoder;
use directories::BaseDirs;
use flate2::read::GzDecoder;
use xz2::read::XzDecoder;
use regex::Regex;
use std::fs;
use std::fs::File;
Expand All @@ -49,6 +50,7 @@ const DMG: &str = "dmg";
const EXE: &str = "exe";
const DEB: &str = "deb";
const MSI: &str = "msi";
const XZ: &str = "xz";
const SEVEN_ZIP_HEADER: &[u8; 6] = b"7z\xBC\xAF\x27\x1C";
const UNCOMPRESS_MACOS_ERR_MSG: &str = "{} files are only supported in macOS";

Expand Down Expand Up @@ -124,6 +126,8 @@ pub fn uncompress(
untargz(compressed_file, target, log)?
} else if extension.eq_ignore_ascii_case(BZ2) {
uncompress_bz2(compressed_file, target, log)?
} else if extension.eq_ignore_ascii_case(XZ) {
uncompress_xz(compressed_file, target, log)?
} else if extension.eq_ignore_ascii_case(PKG) {
uncompress_pkg(compressed_file, target, log)?
} else if extension.eq_ignore_ascii_case(DMG) {
Expand Down Expand Up @@ -339,6 +343,28 @@ pub fn uncompress_bz2(compressed_file: &str, target: &Path, log: &Logger) -> Res
Ok(())
}

pub fn uncompress_xz(compressed_file: &str, target: &Path, log: &Logger) -> Result<(), Error> {
log.trace(format!(
"Uncompress {} to {}",
compressed_file,
target.display()
));
let mut xz_decoder = XzDecoder::new(File::open(compressed_file)?);
let mut buffer: Vec<u8> = Vec::new();
xz_decoder.read_to_end(&mut buffer)?;
let mut archive = Archive::new(Cursor::new(buffer));
if !target.exists() {
for entry in archive.entries()? {
let mut entry_decoder = entry?;
let entry_path: PathBuf = entry_decoder.path()?.iter().skip(1).collect();
let entry_target = target.join(entry_path);
fs::create_dir_all(entry_target.parent().unwrap())?;
entry_decoder.unpack(entry_target)?;
}
}
Ok(())
}

pub fn unzip(
compressed_file: &str,
target: &Path,
Expand Down

0 comments on commit ffb2a9e

Please sign in to comment.