borisbrodski / sevenzipjbinding

7-Zip-JBinding
http://sevenzipjbind.sourceforge.net/
Other
179 stars 51 forks source link

Exception happened when it run in multi Thread (java) #46

Closed dwj1979 closed 3 years ago

dwj1979 commented 3 years ago

when run in multi thread , HEADERS_ERROR or DATA_ERROR returned.

borisbrodski commented 3 years ago

My test set contains hundreds of tests, that run extraction and compression extensively and multithreaded. It should be some problem with your code.

Please make sure, you exact one archive in one thread, but be aware, that some stream read/write callbacks my be called in a different (new) thread, created by 7-Zip engine.

dwj1979 commented 3 years ago

i want to exact one archive in multithread for increase exact speed. one archive item one thread (using thread pool). by the way this [sevenzipjbinding] exact speed very slow, have you other sulotion to increase exact speed

borisbrodski commented 3 years ago

You can extract a single archive in a multithreaded way, if you in each thread:

The 7-zip engine should not get calls to a single opened archive from multiple threads. But nothing stops you to open in each thread the same archive "pretending" that these are different archives.

Extraction speeds a regulary much higher compared to native java implementations. 7-zip engine is actually very fast and efficient. The C++ <-> Java bridge overhead is very low, if you don't have insane amount of very small archive items.

I'm closing this issue, as this is not a bug. Fill free to continue post messages here, if you need help or reopen it, if you feel, it is a bug.

dwj1979 commented 3 years ago

close is OK. thank you for help.

about speed. test result: environment: i7, SSD, single Thread achive format: zip (using same file)

1) using java.util.zip.ZipFile, java.util.zip.ZipEntry Speed: 108M/s 2) using sevenzipjbinding Speed: 62M/s

have you any method to increase speed.

borisbrodski commented 3 years ago

Interesting... Could you please:

dwj1979 commented 3 years ago

My enviroment: CPU Intel i7-6700 3.4GHz OS: windows server 2019 64 Bit Java: jdk-11.0.9

My Test Code:

` public static void un7z(String file7zPath, final String outPutPath) { long start = new Date().getTime(); File outPutPathFile = new File(outPutPath); if (!outPutPathFile.exists()) { outPutPathFile.mkdirs(); } RandomAccessFile randomAccessFile = null; RandomAccessFileInStream randomAccessFileInStream = null; IInArchive archive = null; try { randomAccessFile = new RandomAccessFile(file7zPath, "r"); randomAccessFileInStream = new RandomAccessFileInStream(randomAccessFile); archive = SevenZip.openInArchive(null, randomAccessFileInStream); ISimpleInArchive simpleInArchive = archive.getSimpleInterface(); ISimpleInArchiveItem[] itemArr = simpleInArchive.getArchiveItems(); for (final ISimpleInArchiveItem item : itemArr) { if (!item.isFolder()) { File ouputFile = null; String str = item.getPath(); ouputFile = new File(outPutPath + File.separator + str); if (str.lastIndexOf(File.separator) > 0) { if (!ouputFile.getParentFile().exists()) { ouputFile.getParentFile().mkdirs(); } }

                 RandomAccessFile raf = new RandomAccessFile(ouputFile, "rw");

                try {
                    ISequentialOutStream iSequentialOutStream = new ISequentialOutStream() {
                        public int write(byte[] data) throws SevenZipException {
                            try {
                                raf.write(data);
                            } catch (Exception e) {
                                throw new SevenZipException(e);
                            }
                            return data.length; // Return amount of consumed
                        }
                    };

                    ExtractOperationResult result = item.extractSlow(iSequentialOutStream);
                    if (result != ExtractOperationResult.OK) {
                        log.error("解压失败:" + result);
                        break;
                    }
                }
                finally {
                    try {
                        raf.close();
                    } catch (Exception e) {}
                }
            }
        }
    }
    catch (FileNotFoundException | SevenZipException e) {
        log.error("不支持的zip格式", e);
    }
    finally {
        try {
            if (archive != null) {
                archive.close();
            }
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
            if (randomAccessFileInStream != null) {
                randomAccessFileInStream.close();
            }
        } catch (Exception de) {}
    }
    long end = new Date().getTime();
    long cost = (end - start)/1000;
    if ( cost == 0) {
         log.info("cost:{}sec", cost);
    } else {
         log.info("cost:{}sec, speed:{}M/s", cost, new File(file7zPath).length()/1024/1024/cost);
    }
}

` Using this code, extract .zip file speed: 62-72MB/s (zip file size: 435 MB) extract .7z file speed: ~~14MB/s (zip file size: 720 MB)

dwj1979 commented 3 years ago

have any infomation to update?