How to release a file in Java GUI without closing

How to release a file in Java GUI without closing



I've created a pretty simple Java GUI to browse/load a zip file on Windows platform to begin unzipping and then do some file checking.



Everything works fine except that I have to close the GUI window in order to delete the zip file that has been opened in the GUI.In my finally block of the unzipping method, I've tried adding the following:


public static String unZip(String path)

int count = -1;
String savepath = "";
File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
savepath = path.substring(0, path.lastIndexOf("\")) + File.separator; //File saving directory
new File(savepath).mkdir(); //create the saving directory
ZipFile zipFile = null;
String topLevelDirName="";
try

zipFile = new ZipFile(path,Charset.forName("gbk")); //Encoding
Enumeration<?> entries = zipFile.entries();
int levelCount=0;
while(entries.hasMoreElements())

byte buf = new byte[buffer];
ZipEntry entry = (ZipEntry)entries.nextElement();
String filename = entry.getName();
boolean ismkdir = false;
if(filename.lastIndexOf("/") != -1) //To check if there is a directory
ismkdir = true;

filename = savepath + filename;
if(entry.isDirectory()) //If it is a directory
levelCount++;
file = new File(filename);
file.mkdirs();
if(levelCount==1)
topLevelDirName = filename;
continue;


file = new File(filename);
if(!file.exists())
if(ismkdir)
new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs();




file.createNewFile(); //Create the file
is = zipFile.getInputStream(entry);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, buffer);
while((count = is.read(buf)) > -1)

bos.write(buf, 0, count);

bos.flush();
bos.close();
fos.close();
is.close();

zipFile.close();
catch(IOException ioe)
ioe.printStackTrace();
finally
try
if(bos != null)
bos.close();

if(fos != null)
fos.close();

if(is != null)
is.close();

if(zipFile != null)
zipFile.close();


catch(Exception e)
e.printStackTrace();

return topLevelDirName;




However, I am still not able to delete the zip unless explicitly close the GUI.



Wonder if there is anything to do with the Windows file handle?Thanks in advance.






Are you using a ZipFile or ZipInputStream? Probably need to close those to. You'd also be better of using a try-with-resources statement

– MadProgrammer
Sep 10 '18 at 7:52


ZipFile


ZipInputStream


try-with-resources






Post some more code, please.

– david a.
Sep 10 '18 at 7:52






@davida.updated

– Kevin
Sep 10 '18 at 7:57






@MadProgrammer yes, it is a ZipFile

– Kevin
Sep 10 '18 at 7:57






In this sample is used a wrapper class named ZipFileSource and override close() method. maybe helpful : stackoverflow.com/questions/36291737/…

– Pasha Gharibi
Sep 10 '18 at 8:04





1 Answer
1



Java 8 introduced the try-with-resources Statement to make this kind of situation simpler and cleaner.



One of the issues you have is, if any one of the attempts to close the many resources you have open fails, then none of the others will be closed


public static String unZip(String path) throws IOException
int count = -1;

File sourceFile = new File(path);
String name = sourceFile.getName();
name = name.substring(0, name.lastIndexOf(".zip"));
File sourcePath = new File(sourceFile.getParent(), name);

System.out.println("SavePath = " + sourcePath);
if (!sourcePath.exists() && !sourcePath.mkdirs())
throw new IOException("Could not create directory " + sourcePath);

String topLevelDirName = "";
try (ZipFile zipFile = new ZipFile(sourceFile))
Enumeration<?> entries = zipFile.entries();
int levelCount = 0;
byte buf = new byte[1024];
while (entries.hasMoreElements())
ZipEntry entry = (ZipEntry) entries.nextElement();
String filename = entry.getName();
File file = new File(sourcePath, filename);
if (entry.isDirectory()) //If it is a directory
levelCount++;
System.out.println("Make directory " + file);
if (!file.exists() && !file.mkdirs())
throw new IOException("Could not create directory " + filename);

else
System.out.println("Extract to " + file);
try (InputStream is = zipFile.getInputStream(entry);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)))
while ((count = is.read(buf)) > -1)
bos.write(buf, 0, count);





return topLevelDirName;



I've update the code slightly to try and make it a little cleaner and simpler and to take advantage of the available APIs






thanks,I've tried your code but it seems still doesn't allow me delete the zip file. Maybe I am missing something?

– Kevin
Sep 10 '18 at 8:33






I don't know, since this all the code you've provided. This example works fine for me, maybe something else is holding the file open? Maybe try restarting the PC

– MadProgrammer
Sep 10 '18 at 8:34






great,actually another service is holding a reference to the zipFile and did not close. Now it works thanks!

– Kevin
Sep 10 '18 at 8:36



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)