I'm curious about the algorithm that processes compressed files in Java

Asked 2 years ago, Updated 2 years ago, 78 views

Hello, everyone~

I'm curious about the algorithm or process of decompressing Java~

Compressed to a ZIP file, when uncompressed from MultipartFile or File

Typically, you will create a new file. (For many sample codes...)

Compressed Files > Uncompressed > Create Files Before Compression (Origin Files)

However, de-extracting is actually to upload the file somewhere else

Eventually, you have to create a temporary file, and I think you have to delete it after uploading it.

I wonder if this is the best way.

Is there any other way... I'm curious.

zip s3 uncompress

2022-09-22 22:01

1 Answers

See Example of JavaDoc first.

If the intent of the question is to transfer some of the zip compressed content without creating a temporary file, we can transform the example above:

For Zip files, you can find files of zip compressed content through the ZipEntry object.

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

public class Sample {
   final int BUFFER = 2048;
   public static void main (String argv[]) {
      try {
         BufferedOutputStream dest = null;
         FileInputStream fis = new 
       FileInputStream(argv[0]);
         ZipInputStream zis = new 
       ZipInputStream(newBufferedInputStream(fis); // Prepare a stream to read Zip compression
         ZipEntry; // Variables to correspond to files contained in ZIP compressed content
         while(((entry = zis.getNextEntry())!=null) { // Search sequentially.
            ...;  
           // If the file you want to find is correct
          // entry.name() is the name that contains the path within the compressed file.           
            int count;
            byte data[] = new byte[BUFFER];
            OutputStreamos = ...; // Stream trying to send decompressed content
            dest = new 
              BufferedOutputStream(os, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) 
              != -1) {
               dest.write(data, 0, count); // transmitted with decompression.
            }
            dest.flush();
            dest.close();
         }
         zis.close();
      } } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

If you do it like this, you can do it without creating a temporary file. If the purpose is to decompress and transfer all contained files, it might be better to create a temporary file.

If you want to compress for a single file stream, I think it would be good to try GZIP compressed stream. GZIP is a compression algorithm that can be processed while compressing (or decompressing) the stream itself, so it can be used without creating a separate temporary file.

OutputStreamos = new ByteArrayOutputStream(); // Stream for writing files, sockets, HTTP, etc. In this example, we did ByteArrayOutputStream.
GZIPOutputStream gout= new GZIPOutputStream(os);

If you write uncompressed data to gout.write(...); // gout, the resulting compression is passed to the target stream (os).

gout.close();

byte[] gzippedData = ...; // data compressed by GZIP
InputStream is = new ByteArrayInputStream(gzippedData); // Stream for reading from a file or socket, HTTP, etc. In this example, we did ByteArrayInputStream.
GZIPInputStream gin = new GZIPInputStream(is);

byte[] buffer = new byte[100];
int length;
while((length = bufferedInputStream.read(buffer)) > 0) {
   ...; // Buffer contains uncompressed content.
}

gin.close();


2022-09-22 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.