borisbrodski / sevenzipjbinding

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

how to close ISequentialOutStream? #42

Closed ThomasYouung closed 3 years ago

ThomasYouung commented 3 years ago

when i use this lib, everything is ok.

But when i want to delete the folder which contains the items i juse decompress, i found that i can not delete successfull,

the command i use to delete folder is rm -rf aaa

this below is the info when i delete folder 图片

borisbrodski commented 3 years ago

Hello Thomas,

7-Zip-JBinding doesn't read from nor write to any file. Also, all opened resources get closed upon Java VM termination. Are you sure, that this problems are related to 7-Zip-JBinding? Maybe corrupted file system or hard drive?

Cheers, Boris

PS I can't read Japanese, very unfortunately....

jinfeihan57 commented 3 years ago
        Actually, this is Chinese.(Because Japanese has borrowed a lot of Chinese.People who don’t understand Chinese can’t distinguish clearly.)The error reported here is that unzipped folder cannot deleted because the device is busy.实际上这是中文。(因为日文引用了很多中文所以不熟悉中文的人会搞不清楚) On 03/03/2021 21:36, Boris Brodski wrote: 

Hello Thomas, 7-Zip-JBinding doesn't read from nor write to any file. Also, all opened resources get closed upon Java VM termination. Are you sure, that this problems are related to 7-Zip-JBinding? Maybe corrupted file system or hard drive? Cheers, Boris PS I can't read Japanese, very unfortunately....

—You are receiving this because you are subscribed to this thread.Reply to this email directly, view it on GitHub, or unsubscribe. [ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/borisbrodski/sevenzipjbinding/issues/42#issuecomment-789718344", "url": "https://github.com/borisbrodski/sevenzipjbinding/issues/42#issuecomment-789718344", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } } ]

ThomasYouung commented 3 years ago

first , thanks a lot for your quickly reply. but i still have some questions to ask.

In my case, i use this tool in my web environment, so the jvm will not termination when i extra a file.

so i want to know if there is a io resource that will not closed when the extra work is done.

when i see again the samples in the web, i can`t understand how the date is get (see the picture below), can you drew a simple map to explain how the data is get?

i think this is the key to solve my problem.

图片

thanks a lot again.

borisbrodski commented 3 years ago

Ok, this is a very simple example not involving any IO. Generally, as I already stated, 7-Zip-JBinding never access any file or directory. It operates in memory only consuming and providing input and output stream. All I/O should be done by the user of the library.

To answer your question, the "data" is allocated as a Java byte array using JNI. The extracted data from 7z engine is then copied into the "data" byte array. The "data" byte array get garbage collected later on, if not referenced. No closing is required.

To help you with your problem, I need to see your code, that saves "data" into the file.

ThomasYouung commented 3 years ago

codes.zip

hello again, i add three files to the codes.zip.

the only difference between the sample and my code is i add a field named filesExtracteds to the MyExtractCallback.java

to expose the file i have extraed.

the codes of 'save' byte to file is in the file of TestExtra.java, no special (smile).

please help me check if there is some wrong that i am not use it correctly.

plus: 1) the platform i use to run the codes is windows. 2) my english is not very good, forgive me please.

borisbrodski commented 3 years ago

You need to close files in this loop

    for (String fileName: resultMap.keySet()) {
        String resultFile = destDic + "\\" +fileName;
        fos = new FileOutputStream(new File(resultFile));
        fos.write(resultMap.get(fileName));

        // TODO Close "fos" here

    }
ThomasYouung commented 3 years ago

oh my god!

First, my question is solved! thanks a looot again.(smile)

it is all my fault, i don not know the way i close io is wrong before. i think i only need to close io object in finally section.

So, is there some good suggestion or resources to learn io ? i realize java io is my weekness.

Report a case: if it is a real question that need to fix?

in my eviroment, i use weblogic as server, there may be not only one app in the weblogic, so when i start two or more apps, the

log-file report the error below:

图片

borisbrodski commented 3 years ago

Right, this is a known issue of 7-Zip-JBinding. It hope to get it fixed in the next release. For now you have to use some proprietary mechanism to ensure, that 7-Zip-JBinding don't get initialized twice. For example, for JBoss (Wildfly) use could use modules, although I haven't tested it myself yet.

Could you please close this issue and open a new one with the last image and short description of the problem?

Closing resources in Java is much more complicated as one could imagine. Here is a blog, that partially describes it: https://javarevisited.blogspot.com/2014/10/right-way-to-close-inputstream-file-resource-in-java.html#axzz6ogSkKENO

I would recommend to use try-for-resource block as much as possible. It ensures, that no resources stay open.

ThomasYouung commented 3 years ago

The image is to show the error when init 7-Zip-JBinding twoice, not else problem.

For now, i will just init one time in the weblogic for one app, other apps / / .

By the way, if there is a plan to fix the problem? have a deadline ?(smile)

Sadly, i am still using jdk1.6 in my product enviroment, it is a bank project.

Anyway, thanks for your help, you give me a big rescue, a big hug for you.