Compress files in separate parts (no need to be just to unpack)

0

In my application I can compress a folder and its contents, this content is the result of manipulation of pdf, jpg and xls. But I needed to split it into pieces (the compressed file can not be larger than 10MB for example), but so that one part can be extracted without the need for the other. I also get another class to pack into parts but with the need for all parts to be together to be able to draw. This class is the one I use to create a single file.

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCompress {
    public static void compress(String dirPath, String zipName,String path) {
        Path sourceDir = Paths.get(dirPath);
        String zipFileName = path+zipName.concat(".zip");
        try {
            ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
            Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
                    try {
                        Path targetFile = sourceDir.relativize(file);
                        outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
                        byte[] bytes = Files.readAllBytes(file);
                        outputStream.write(bytes, 0, bytes.length);
                        outputStream.closeEntry();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Update:

I added the method I'm working on to first separate the files to each "x mb" and then compress the folders individually. I have not yet been able to put in a loop where each "x mb" is created a new folder and the files are then copied to this new folder.

Another detail is that I realized that it reaches 10 MB my method to copy the files even though it is being used if (fileSizeInMB > 5) , it should be the way it is, copy all. (Solved changing from: if (fileSizeInMB > 5) to if (fileSizeInMB >= 5) , updated method in question.

Update:

Resolved to list the files in the folder (folder containing the files I want to compress), after this create a new folder and move the files until the cumulative size is "x MB", each time the cumulative size of the files is greater than "x mb" create a new folder and move the files to this new folder. The code can certainly be improved, I accept critical suggestions.

public void listNovo() throws IOException {
File directory = new File("D:\Faturas\Gerados\");
String pasta = "\Pasta";
long length = 0;
long fileSizeInBytes = 0;
long fileSizeInMB = 0;
long fileSizeInKB = 0;
int count = 0;

File[] files = directory.listFiles();
for (File file : directory.listFiles()) {
    String fileName1 = file.getName();
    if (file.isFile()) {
        length += file.length();
        System.out.println("file:" + file.getName());
        fileSizeInBytes = length;
        fileSizeInKB = fileSizeInBytes / 1024;
        fileSizeInMB = fileSizeInKB / 1024;
        System.out.println("fileSizeInMB:" + fileSizeInMB);
        if (fileSizeInMB < 5) {
            File diretorio = new File(jTCaminho.getText() + pasta + count);
            if (!diretorio.exists()) {
                new File(jTCaminho.getText() + pasta + count).mkdir();
                Files.move(file.toPath(), Paths.get(diretorio.toString(), fileName1), REPLACE_EXISTING);
                length += fileSizeInMB;
            }
            else if (diretorio.exists()) {
                Files.move(file.toPath(), Paths.get(diretorio.toString(), fileName1), REPLACE_EXISTING);
                length += fileSizeInMB;
            }
        }

        if (fileSizeInMB >= 5) {
            fileSizeInMB = 0;
            length=0;
               count++;
            if (fileSizeInMB < 5) {
                File diretorio = new File(jTCaminho.getText() + pasta + count);
                if (!diretorio.exists()) {
                    new File(jTCaminho.getText() + pasta + count).mkdir();
                    Files.move(file.toPath(), Paths.get(diretorio.toString(), fileName1), REPLACE_EXISTING);
                    length += fileSizeInMB;
                }
               else if (diretorio.exists()) {
                    Files.move(file.toPath(), Paths.get(diretorio.toString(), fileName1), REPLACE_EXISTING);
                    length += fileSizeInMB;
                }
            }
        }
    }//fim isFile
}//fim directoryListfile

}

    
asked by anonymous 16.11.2018 / 00:40

0 answers