문제

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