Skip to content

Commit 16255a5

Browse files
committed
[rust] Add XZ uncompressor
Add a XZ uncompressor to uncompress the new Firefox nightly builds for Linux. This is based on the BZ2 uncompressor.
1 parent dbf20f0 commit 16255a5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

rust/src/files.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use apple_flat_package::PkgReader;
2727
use bzip2::read::BzDecoder;
2828
use directories::BaseDirs;
2929
use flate2::read::GzDecoder;
30+
use xz2::read::XzDecoder;
3031
use regex::Regex;
3132
use std::fs;
3233
use std::fs::File;
@@ -49,6 +50,7 @@ const DMG: &str = "dmg";
4950
const EXE: &str = "exe";
5051
const DEB: &str = "deb";
5152
const MSI: &str = "msi";
53+
const XZ: &str = "xz";
5254
const SEVEN_ZIP_HEADER: &[u8; 6] = b"7z\xBC\xAF\x27\x1C";
5355
const UNCOMPRESS_MACOS_ERR_MSG: &str = "{} files are only supported in macOS";
5456

@@ -124,6 +126,8 @@ pub fn uncompress(
124126
untargz(compressed_file, target, log)?
125127
} else if extension.eq_ignore_ascii_case(BZ2) {
126128
uncompress_bz2(compressed_file, target, log)?
129+
} else if extension.eq_ignore_ascii_case(XZ) {
130+
uncompress_xz(compressed_file, target, log)?
127131
} else if extension.eq_ignore_ascii_case(PKG) {
128132
uncompress_pkg(compressed_file, target, log)?
129133
} else if extension.eq_ignore_ascii_case(DMG) {
@@ -339,6 +343,28 @@ pub fn uncompress_bz2(compressed_file: &str, target: &Path, log: &Logger) -> Res
339343
Ok(())
340344
}
341345

346+
pub fn uncompress_xz(compressed_file: &str, target: &Path, log: &Logger) -> Result<(), Error> {
347+
log.trace(format!(
348+
"Uncompress {} to {}",
349+
compressed_file,
350+
target.display()
351+
));
352+
let mut xz_decoder = XzDecoder::new(File::open(compressed_file)?);
353+
let mut buffer: Vec<u8> = Vec::new();
354+
xz_decoder.read_to_end(&mut buffer)?;
355+
let mut archive = Archive::new(Cursor::new(buffer));
356+
if !target.exists() {
357+
for entry in archive.entries()? {
358+
let mut entry_decoder = entry?;
359+
let entry_path: PathBuf = entry_decoder.path()?.iter().skip(1).collect();
360+
let entry_target = target.join(entry_path);
361+
fs::create_dir_all(entry_target.parent().unwrap())?;
362+
entry_decoder.unpack(entry_target)?;
363+
}
364+
}
365+
Ok(())
366+
}
367+
342368
pub fn unzip(
343369
compressed_file: &str,
344370
target: &Path,

0 commit comments

Comments
 (0)