简体   繁体   中英

Java Apache Commons Compress 7zip, corrupt file error

7 zip files (lzma) format, want to compress and encrypt.
I want to use Apache Commons Compress.
I am using this function, however, the decompressed files are corrupted.

Thanks

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());

    byte fileContent[] = new byte[(int)entryFile.length()];

    sevenZOutput.putArchiveEntry(entry);
    sevenZOutput.write(fileContent);
    sevenZOutput.closeArchiveEntry();
    sevenZOutput.close();         
}

I figured, okay, thank you. How do I encryption the archive?

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();    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM