Java and Linux script operations: how to compress and decompress files
Java and Linux script operations: file compression and decompression
Overview:
File compression and decompression are things we often encounter in daily computer operations task. Whether in Java programs or scripts in Linux environments, file compression and decompression are very common requirements. In this article, we will introduce how to use Java and Linux scripts to implement file compression and decompression operations, and give specific code examples.
1. Java implements file compression and decompression:
Java provides a series of classes and methods for file compression and decompression. The following is a sample code for file compression and decompression using Java:
- File compression:
import java.io.*; import java.util.zip.*; public class FileCompression { public static void compress(File source, File destination) throws IOException { FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(destination); ZipOutputStream zos = new ZipOutputStream(fos); zos.putNextEntry(new ZipEntry(source.getName())); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); zos.close(); fis.close(); fos.close(); } public static void main(String[] args) { File source = new File("path/to/source/file"); File destination = new File("path/to/destination/file.zip"); try { compress(source, destination); System.out.println("File compression completed successfully."); } catch (IOException e) { e.printStackTrace(); } } }
- File decompression:
import java.io.*; import java.util.zip.*; public class FileDecompression { public static void decompress(File source, File destination) throws IOException { FileInputStream fis = new FileInputStream(source); ZipInputStream zis = new ZipInputStream(fis); FileOutputStream fos = new FileOutputStream(destination); ZipEntry entry = zis.getNextEntry(); byte[] buffer = new byte[1024]; int length; while ((length = zis.read(buffer)) > 0) { fos.write(buffer, 0, length); } zis.closeEntry(); zis.close(); fis.close(); fos.close(); } public static void main(String[] args) { File source = new File("path/to/source/file.zip"); File destination = new File("path/to/destination/file"); try { decompress(source, destination); System.out.println("File decompression completed successfully."); } catch (IOException e) { e.printStackTrace(); } } }
2. Linux script to realize file compression and decompression:
In Linux environment, we can use shell script to realize file compression and decompression. The following is a sample code for file compression and decompression using a Linux shell script:
- File compression:
#!/bin/bash source="path/to/source/file" destination="path/to/destination/file.tar.gz" tar -czf $destination $source echo "File compression completed successfully."
- File decompression:
#!/bin/bash source="path/to/source/file.tar.gz" destination="path/to/destination/file" tar -xzf $source -C $destination echo "File decompression completed successfully."
It should be noted that in Linux, we use the tar command to compress and decompress files. The -c parameter indicates creation (tar up a file or directory), the -z parameter indicates compression (gzip), the -x parameter indicates decompression (extract files from an archive), and the -f parameter indicates files.
Summary:
This article briefly introduces how to use Java and Linux scripts to implement file compression and decompression operations, and gives specific code examples. Through these sample codes, I hope readers can better understand how to use Java and Linux scripts to handle file compression and decompression needs in actual projects. Of course, this is just a basic sample code, and other issues such as exception handling may need to be considered in actual applications. If necessary, readers can further modify and improve the code according to their actual situation.
The above is the detailed content of Java and Linux script operations: how to compress and decompress files. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use