1. import os
  2. import zstandard as zstd
  3. def unpack_zst(input_file, output_file=None):
  4. """
  5. Unpacks a .zst file using the Zstandard library.
  6. Args:
  7. input_file (str): Path to the input .zst file.
  8. output_file (str): Path to the output file. If None, the output file
  9. will have the same name as the input file but without the .zst extension.
  10. Returns:
  11. str: Path to the decompressed file.
  12. """
  13. if not input_file.endswith('.zst'):
  14. raise ValueError("Input file must have a .zst extension")
  15. if output_file is None:
  16. output_file = input_file.rsplit('.zst', 1)[0]
  17. try:
  18. with open(input_file, 'rb') as compressed_file:
  19. dctx = zstd.ZstdDecompressor()
  20. with open(output_file, 'wb') as decompressed_file:
  21. dctx.copy_stream(compressed_file, decompressed_file)
  22. print(f"Decompressed '{input_file}' to '{output_file}'")
  23. return output_file
  24. except Exception as e:
  25. print(f"Failed to decompress '{input_file}': {e}")
  26. raise
  27. if __name__ == "__main__":
  28. import argparse
  29. parser = argparse.ArgumentParser(description="Unpack a .zst file")
  30. parser.add_argument("input_file", help="Path to the .zst file")
  31. parser.add_argument(
  32. "-o", "--output_file",
  33. help="Path to the output file (default: same name as input without .zst)"
  34. )
  35. args = parser.parse_args()
  36. try:
  37. unpack_zst(args.input_file, args.output_file)
  38. except Exception as e:
  39. print(f"Error: {e}")