Home Java javaTutorial Java and Linux script operations: how to compress and decompress files

Java and Linux script operations: how to compress and decompress files

Oct 05, 2023 pm 05:54 PM
linux java File compression and decompression

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:

  1. 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();
        }
    }
}
Copy after login
  1. 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();
        }
    }
}
Copy after login

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:

  1. 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."
Copy after login
  1. 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."
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

See all articles