Download ZIP from Amazon S3 does not preserve timestamp when extracting in Java -
when download zip file amazon s3 , extract using java, not preserve original timestamp of file inside zip. why? here's uncompress java code:
public void unzipfile(string zipfile, string newfile) { try { fileinputstream fis = new fileinputstream(zipfile); bufferedinputstream bis = new bufferedinputstream(fis); zipinputstream zis = new zipinputstream(bis); fileoutputstream fos = new fileoutputstream(newfile); final byte[] buffer = new byte[1024]; int len = 0; while ((len = zis.read(buffer)) != -1) { fos.write(buffer, 0, len); } //close resources fos.close(); zis.close(); } catch (ioexception e) { e.printstacktrace(); } }
basically, want timestamp of file inside of zip file, file x has jan-01-2010 preserved. file x's overwridden timestamp of zip file, has sep-20-2013.
it's because putting contents of zip file new file.
you try like:
public void unzipfile(string zipfile, string outputfolder){ try { byte[] buffer = new byte[1024]; file folder = new file(outputfolder); if(!folder.exists()){ folder.mkdir(); } zipinputstream zis = new zipinputstream(new fileinputstream(zipfile)); zipentry ze = zis.getnextentry(); while(ze!=null){ string filename = ze.getname(); file newfile = new file(outputfolder + file.separator + filename); //create non exists folders //else hit filenotfoundexception compressed folder new file(newfile.getparent()).mkdirs(); fileoutputstream fos = new fileoutputstream(newfile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); newfile.setlastmodified(ze.gettime()); ze = zis.getnextentry(); } zis.closeentry(); zis.close(); } catch (ioexception e) { e.printstacktrace(); } }
pulled from: http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/ modifications add modified time.
Comments
Post a Comment