SoFunction
Updated on 2025-03-03

Detailed explanation of the steps to process compressed files in Java

background

Upload the file in the project, which contains the compressed package and parses and saves the contents of the compressed package.

Step 1: Write code

1.1 Request layer

We write in flashbacks. Write firstZipController

	@Autowired
    private ZipService zipService;

    /**
      * Upload QR code file
      * @param qrCodeFile QR code file
      * @return Return the uploaded result
      */
    @ApiOperation(value = "Upload QR code file")
    @PostMapping("/uploadQrCodeFile")
    public Result uploadQrCodeFile(@RequestParam("file") MultipartFile qrCodeFile) throws Exception {
        (qrCodeFile);
        return ("Uploaded successfully");
    }

1.2 Business Processing Layer

Then writeService

@Service
public class ZipService {


    private static final Logger logger= ();


    public void uploadQrCodeFile(MultipartFile multipartFile)throws Exception {
        if (() == 0
                || () == null
                || (() != null
                && !().contains("."))) {
            (("The file format is incorrect or the file is empty!"));
        }
        // 1. Download the file to the local area first        String originalFilename = ();
        String destPath = ("") +  + "qrCodeFile";
        (
                (), new File(destPath +  + originalFilename));

        // 2. Unzip the file        unzipAndSaveFileInfo(originalFilename, destPath);
        // 3. Back up the compressed file and delete the unzipped directory        (
                new File(destPath +  + originalFilename),
                new File(destPath +  + "backup" +  + originalFilename));
        // Delete the original uploaded temporary compressed package        (new File(destPath +  + originalFilename));
        ("File upload successfully,The file name is:{}", originalFilename);


    }

    /**
      * Unzip and save file information
      *
      * @param originalFilename Source file name
      * @param destPath Destination path
      */
    private void unzipAndSaveFileInfo(String originalFilename, String destPath) throws IOException {
        if ((originalFilename) || !(".")) {
            (("Error file name!"));
        }
        // Compression        (
                new File(destPath +  + originalFilename),
                new File(destPath),
                ("GBK"));
        // traverse file information        String fileName = (0, ("."));
        File[] files = (destPath +  + fileName);
        if ( == 0) {
            (("Upload file is empty!"));
        }
        String targetPath = destPath +  + "images";
        for (File file : files) {
            // Copy the file to the specified directory            String saveFileName =
                    () + new SecureRandom().nextInt(100) + ();
            (file, new File(targetPath +  + saveFileName));
            ("File Name:"+());
            ("Directory address of the file:"+saveFileName);
            ("Directory address of the file:"+targetPath +  + saveFileName);
        }
    }
}

1.3 Added configuration

Because spring boot has a default upload file size limit, the file size needs to be configured. existAdded inuploadConfiguration

#### upload begin  ###
=true
-request-size=10MB
-file-size=10MB
#### upload end  ###

Step 2: Decompression processing

2.1 Introducing dependencies

IntroduceApache Decompression/Compression Tool-like processing, decompressiondocument

<!-- /artifact//commons-compress -->
<dependency>
    <groupId></groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.20</version>
</dependency>

2.2 Decompression tool class

  • WillConvert totar
  • Unziptar
import ;
import ;
import ;
import ;
import org.;
import org.;
import org.;
import org.;
import ;
import ;
import ;
import ;
import ;


       // File path        String sourcePath = "D:\\";
        // Directory to be unzipped        String extractPath = "D:\\test\\daleyzou";
        File sourceFile = new File(sourcePath);
        // decompressing *. files to tar
        TarArchiveInputStream fin = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(sourceFile)));
        File extraceFolder = new File(extractPath);
        TarArchiveEntry entry;
        // Unzip the tar file into the extractPath directory        while ((entry = ()) != null) {
            if (()) {
                continue;
            }
            File curfile = new File(extraceFolder, ());
            File parent = ();
            if (!()) {
                ();
            }
            // Write the file to the decompressed directory            (fin, new FileOutputStream(curfile));
        }

Summarize

This is the end of this article about the detailed explanation of the steps for Java to process compressed files. For more related contents of Java to process compressed files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!