Skip to content

Commit

Permalink
fix bug: output compressed file automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkLoc committed May 17, 2024
1 parent 2a6d88e commit ef61857
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ fn is_gzipped<P: AsRef<Path> + Copy>(file_name: P) -> Result<bool> {
let buffer = magic_num(file_name)?;
let gz_or_not =
buffer[0] == GZ_MAGIC[0] && buffer[1] == GZ_MAGIC[1] && buffer[2] == GZ_MAGIC[2];
Ok(gz_or_not || file_name.as_ref().ends_with(".gz"))
Ok(gz_or_not || file_name.as_ref().extension().is_some_and(|ext| ext == "gz"))
}

fn is_bzipped<P: AsRef<Path> + Copy>(file_name: P) -> Result<bool> {
let buffer = magic_num(file_name)?;
let bz_or_not =
buffer[0] == BZ_MAGIC[0] && buffer[1] == BZ_MAGIC[1] && buffer[2] == BZ_MAGIC[2];
Ok(bz_or_not || file_name.as_ref().ends_with(".bz2"))
Ok(bz_or_not || file_name.as_ref().extension().is_some_and(|ext| ext == "bz2"))
}

fn is_xz<P: AsRef<Path> + Copy>(file_name: P) -> Result<bool> {
Expand All @@ -44,7 +44,7 @@ fn is_xz<P: AsRef<Path> + Copy>(file_name: P) -> Result<bool> {
&& buffer[3] == XZ_MAGIC[3]
&& buffer[4] == XZ_MAGIC[4]
&& buffer[5] == XZ_MAGIC[5];
Ok(xz_or_not || file_name.as_ref().ends_with(".xz"))
Ok(xz_or_not || file_name.as_ref().extension().is_some_and(|ext| ext == "xz"))
}

pub fn file_reader<P>(file_in: Option<P>) -> Result<Box<dyn BufRead>>
Expand Down Expand Up @@ -95,17 +95,17 @@ where
let fp = File::create(file_name)
.map_err(FqkitError::IoError)?;

if file_name.as_ref().ends_with(".gz") {
if file_name.as_ref().extension().is_some_and(|ext| ext == "gz") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
flate2::write::GzEncoder::new(fp, flate2::Compression::new(compression_level)),
)))
} else if file_name.as_ref().ends_with(".bz2") {
} else if file_name.as_ref().extension().is_some_and(|ext| ext == "bz2") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
bzip2::write::BzEncoder::new(fp, bzip2::Compression::new(compression_level)),
)))
} else if file_name.as_ref().ends_with(".xz") {
} else if file_name.as_ref().extension().is_some_and(|ext| ext == "xz") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
xz2::write::XzEncoder::new(fp, compression_level),
Expand All @@ -128,17 +128,17 @@ where
.open(file_out)
.map_err(FqkitError::IoError)?;

if file_out.as_ref().ends_with(".gz") {
if file_out.as_ref().extension().is_some_and(|ext| ext == "gz") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
flate2::write::GzEncoder::new(fp, flate2::Compression::new(compression_level)),
)))
} else if file_out.as_ref().ends_with(".bz2") {
} else if file_out.as_ref().extension().is_some_and(|ext| ext == "bz2") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
bzip2::write::BzEncoder::new(fp, bzip2::Compression::new(compression_level)),
)))
} else if file_out.as_ref().ends_with(".xz") {
} else if file_out.as_ref().extension().is_some_and(|ext| ext == "xz") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
xz2::write::XzEncoder::new(fp, compression_level),
Expand Down

0 comments on commit ef61857

Please sign in to comment.