欢迎访问宙启技术站
智能推送

Java函数中如何实现文件的压缩和解压缩操作?

发布时间:2023-07-03 02:47:06

在Java中,可以使用Java自带的ZipInputStream和ZipOutputStream类来实现文件的压缩和解压缩操作。ZipInputStream用于读取zip文件中的压缩数据,而ZipOutputStream用于将压缩数据写入到zip文件中。

以下是文件压缩的代码示例:

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileCompression {

    public static void main(String[] args) {
        String sourceFilePath = "path/to/source/file";
        String zipFilePath = "path/to/zip/file.zip";
        
        compress(sourceFilePath, zipFilePath);
    }

    public static void compress(String sourceFilePath, String zipFilePath) {
        try {
            File sourceFile = new File(sourceFilePath);
            FileOutputStream fos = new FileOutputStream(zipFilePath);
            ZipOutputStream zos = new ZipOutputStream(fos);
            
            compressFile(sourceFile, sourceFile.getName(), zos);
            
            zos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void compressFile(File sourceFile, String fileName, ZipOutputStream zos) throws IOException {
        if (sourceFile.isDirectory()) {
            File[] files = sourceFile.listFiles();
            if (files != null) {
                for (File file : files) {
                    compressFile(file, fileName + File.separator + file.getName(), zos);
                }
            }
        } else {
            byte[] buffer = new byte[1024];
            FileInputStream fis = new FileInputStream(sourceFile);
            
            zos.putNextEntry(new ZipEntry(fileName));
            
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }
            
            zos.closeEntry();
            fis.close();
        }
    }
}

以上代码中的compress方法用于执行文件压缩操作。首先,我们创建一个File对象表示源文件。然后,通过FileOutputStream创建一个文件输出流,以将压缩的数据写入到zip文件中。接下来,我们创建一个ZipOutputStream对象,将文件输出流传递给它,这将允许我们将文件数据写入zip文件。在compressFile方法中,我们使用递归的方式遍历源文件夹中的所有文件和子文件夹。如果遇到一个文件夹,我们递归地调用compressFile方法处理它;如果遇到一个文件,我们使用FileInputStream读取文件数据,并通过ZipOutputStream的putNextEntry方法将文件数据写入到zip文件中。

以下是文件解压缩的代码示例:

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FileDecompression {

    public static void main(String[] args) {
        String zipFilePath = "path/to/zip/file.zip";
        String destinationFolder = "path/to/destination/folder";
        
        decompress(zipFilePath, destinationFolder);
    }

    public static void decompress(String zipFilePath, String destinationFolder) {
        try {
            File folder = new File(destinationFolder);
            if (!folder.exists()) {
                folder.mkdir();
            }
            
            FileInputStream fis = new FileInputStream(zipFilePath);
            ZipInputStream zis = new ZipInputStream(fis);
            
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                String entryName = entry.getName();
                File entryFile = new File(destinationFolder + File.separator + entryName);
                
                if (entry.isDirectory()) {
                    entryFile.mkdirs();
                } else {
                    FileOutputStream fos = new FileOutputStream(entryFile);
                    
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, length);
                    }
                    
                    fos.close();
                }
            }
            
            zis.closeEntry();
            zis.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以上代码中的decompress方法用于执行文件解压缩操作。首先,我们创建一个File对象表示目标文件夹。如果目标文件夹不存在,我们使用mkdir方法创建它。然后,我们使用FileInputStream创建一个文件输入流,以读取zip文件中的压缩数据。接下来,我们创建一个ZipInputStream对象,将文件输入流传递给它,这将允许我们从zip文件中读取压缩数据。在while循环中,我们使用ZipInputStream的getNextEntry方法逐个获取zip文件中的文件。如果获取的是一个文件夹,我们使用mkdirs方法创建它;如果获取的是一个文件,我们使用FileOutputStream创建一个文件输出流,并使用ZipInputStream的read方法将该文件的数据写入文件输出流。最后,我们关闭文件输出流和ZipInputStream。

这样,我们就可以在Java中实现文件的压缩和解压缩操作了。