Вопрос

I'm using Apache Commons Compress to compress files. How do I add a password to the archive so?

public static void main(String args[]) throws FileNotFoundException, IOException {
SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File("outFile.7z"));
File entryFile = new File("D:/image.jpg");
SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(entryFile, entryFile.getName());
sevenZOutput.putArchiveEntry(entry);
FileInputStream in = new FileInputStream(entryFile);
                int len;
                byte buffer[] = new byte[8192];
                int transferedMegaBytes2=0;
                while ((len = in.read(buffer)) > 0) {
                    sevenZOutput.write(buffer, 0, len);                    
                    transferredBytes += len;
                    int transferedMegaBytes = (int) (transferredBytes / 1048576);                          
                    if(transferedMegaBytes>transferedMegaBytes2){
                    System.out.println("Transferred: " + transferedMegaBytes + " Megabytes.");
                    transferedMegaBytes2=transferedMegaBytes;
                    }
                }
sevenZOutput.closeArchiveEntry();
sevenZOutput.close();    
}
Это было полезно?

Решение

I am afraid the compression is not supported. You may want to use this JNI wrapper.

If you do that, you'll probably lose the platform independence. (They say it is cross-platform, but I wouldn't bet on it)

Другие советы

I don't think you can using Commons Compress. From the examples section of the Apache Commons Compress site:

We currently only provide read support for lzma, arj, dump and Z. arj can only read uncompressed archives, 7z can read archives with many compression and encryption algorithms supported by 7z but doesn't support encryption when writing archives.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top