spring mvc - java.util.zip.ZipEntry and apache compress ZipEntry corrupt some image files -


i have been using spring's multipart uploader controllers upload , store entries zipped files, finding occaisional png file being corrupted, instead of begginning "png..." in byte[], starts "fþ»ÀÃgÞÉ" or similar. seems happen same files on each run. tried of using java.util.zipentry , tried apache compress , found apache compress corrupted different files java 7 utility, same files on subsequent runs.

the code (firstly java.util.zip.zipentry):

protected void processzipfile(string path, multipartfile file, string signature) throws ioexception {      dateformat df = new simpledateformat("yyyymmddhhmmss");     file tempfile = new file(system.getproperty("user.dir") + "/" + file.getname() + df.format(new date()));     file.transferto(tempfile);      zipfile zipfile = null;     try {         zipfile = new zipfile(tempfile);          log.debug("processing archive name={}, size={}.", file.getname(), file.getsize());          final enumeration<? extends zipentry> entries = zipfile.entries();         while ( entries.hasmoreelements() )         {             zipentry entry = entries.nextelement();              log.debug("processing file={} directory?={}.", entry.getname(), entry.isdirectory());              // don't bother processing directories, , don't process resource fork info             // mac os x (which not seem transparent zipfile).              if (!(entry.isdirectory() || entry.getname().contains("__macosx")  || entry.getname().contains(".ds_store"))) {                 // if entry file, extract                   content contenttosave = null;                  if(entry.getname().contains("gif") || entry.getname().contains("png") || entry.getname().contains("jpeg")) {                      byte[] bytes = readinputstream( zipfile.getinputstream( entry ), entry.getsize() );                      log.debug("{} of inflated-length={} compressed-length={}",                             entry.getname(), bytes.length, entry.getcompressedsize());                      if(entry.getname().contains("gif")) {                         contenttosave = content.makeimage(path + entry.getname(), content.gif, signature, bytes);                      } else if (entry.getname().contains("png")) {                         contenttosave = content.makeimage(path + entry.getname(), content.png, signature, bytes);                      } else if (entry.getname().contains("jpeg")) {                         contenttosave = content.makeimage(path + entry.getname(), content.jpeg, signature, bytes);                      }                 } else {                      inputstream = zipfile.getinputstream(entry);                      if (entry.getname().contains("json")) {                         contenttosave = content.makefile(path + entry.getname(), content.json, signature, convertstreamtostring(is));                     } else if (entry.getname().contains("js")) {                         contenttosave = content.makefile(path + entry.getname(), content.js, signature, convertstreamtostring(is));                     } else if (entry.getname().contains("css")) {                         contenttosave = content.makefile(path + entry.getname(), content.css, signature, convertstreamtostring(is));                     } else if (entry.getname().contains("xml")) {                         contenttosave = content.makefile(path + entry.getname(), content.xml, signature, convertstreamtostring(is));                     } else if (entry.getname().contains("html")) {                         contenttosave = content.makefile(path + entry.getname(), content.html, signature, convertstreamtostring(is));                     }                 }                  contentservice.putorreplace(contenttosave);                 log.info("persisted file: {} uploaded version.", contenttosave.getname());             }          }     } catch (zipexception e) {         // if can't create zipfile, not zip file @ , cannot processed         // method. pretty dumb there's no way determine whether contents zipped through         // zipfile api, that's 1 of many problems.         e.printstacktrace();         log.error("{} not zipped file, or empty", file.getname());     } {         zipfile = null;     }     tempfile.delete();  } 

and same thing org.apache.commons.compress.archivers.zip.zipfile:

protected void processzipfile(string path, multipartfile file, string signature) throws ioexception {      dateformat df = new simpledateformat("yyyymmddhhmmss");     file tempfile = new file(system.getproperty("user.dir") + "/" + file.getname() + df.format(new date()));     file.transferto(tempfile);      zipfile zipfile = null;     try {         zipfile = new zipfile(tempfile);          log.debug("processing archive name={}, size={}.", file.getname(), file.getsize());          final enumeration<? extends ziparchiveentry> entries = zipfile.getentries();         while ( entries.hasmoreelements() ) {             ziparchiveentry entry = entries.nextelement();              log.debug("processing file={} directory?={}.", entry.getname(), entry.isdirectory());                  // don't bother processing directories, , don't process resource fork info                 // mac os x (which not seem transparent zipfile).                  if (!(entry.isdirectory() || entry.getname().contains("__macosx")  || entry.getname().contains(".ds_store"))) {                     // if entry file, extract                      content contenttosave = null;                      if(entry.getname().contains("gif") || entry.getname().contains("png") || entry.getname().contains("jpeg")) {                          byte[] bytes = readinputstream( zipfile.getinputstream( entry ), entry.getsize() );                          log.debug("{} of inflated-length={} compressed-length={}",                             entry.getname(), bytes.length, entry.getcompressedsize());                          if(entry.getname().contains("gif")) {                             contenttosave = content.makeimage(path + entry.getname(), content.gif, signature, bytes);                          } else if (entry.getname().contains("png")) {                             contenttosave = content.makeimage(path + entry.getname(), content.png, signature, bytes);                          } else if (entry.getname().contains("jpeg")) {                             contenttosave = content.makeimage(path + entry.getname(), content.jpeg, signature, bytes);                          }                     } else {                          inputstream = zipfile.getinputstream(entry);                          if (entry.getname().contains("json")) {                             contenttosave = content.makefile(path + entry.getname(), content.json, signature, convertstreamtostring(is));                         } else if (entry.getname().contains("js")) {                             contenttosave = content.makefile(path + entry.getname(), content.js, signature, convertstreamtostring(is));                         } else if (entry.getname().contains("css")) {                             contenttosave = content.makefile(path + entry.getname(), content.css, signature, convertstreamtostring(is));                         } else if (entry.getname().contains("xml")) {                             contenttosave = content.makefile(path + entry.getname(), content.xml, signature, convertstreamtostring(is));                         } else if (entry.getname().contains("html")) {                             contenttosave = content.makefile(path + entry.getname(), content.html, signature, convertstreamtostring(is));                         }                     }                      contentservice.putorreplace(contenttosave);                     log.info("persisted file: {} uploaded version.", contenttosave.getname());                 }          }     } catch (zipexception e) {         e.printstacktrace();         log.error("{} not zipped file, or empty", file.getname());     } catch (ioexception e) {         e.printstacktrace();         log.error("{} not file, or empty", file.getname());     } {         zipfile = null;     }     tempfile.delete(); } 

the 2 called methods are:

private static byte[] readinputstream( final inputstream is, final long length ) throws ioexception {     final byte[] buf = new byte[ (int) length ];     int read = 0;     int cntread;     while ( ( cntread = is.read( buf, 0, buf.length ) ) >=0  )     {         read += cntread;     }     return buf; } 

and:

public string convertstreamtostring(inputstream is) throws ioexception {     stringbuilder sb = new stringbuilder(2048);     char[] read = new char[128];     try (inputstreamreader ir = new inputstreamreader(is, standardcharsets.utf_8)) {         (int i; -1 != (i = ir.read(read)); sb.append(read, 0, i));     }      // need remove ? @ teh beginning of files. comes utf8 bom     // added files saved utf8     string out = sb.tostring();     string utf8bom = new string(new char[]{'\ufeff'});     if(out.contains(utf8bom)) {         out = out.replace(utf8bom,"");     }     return out; } 

the second 1 is, of course, not part of problem.

i have googled around , looks issues similar have been found, been outside issue. know why might case?

i have re-edited images , found if change image black , white, or change hue of whole image, problem goes away, if add border or change single colour problem remains. looks particular arrangement of bytes in files tickles bug in whatever underlying api both java's own , apache's compressed file readers use, that's speculation.

edit: additional usage shows corruption happens in gifs on 10k in size, perhaps has bug? have tried arbitrarily doubling size of buffer in call readinputstream(), did nothing except overflow blob size in mysql in particularly large images (49k became 98k, big).

com.mysql.jdbc.mysqldatatruncation: data truncation: data long column 'encoded_content' @ row 1 

my finding issue arise when 'packed size' larger actual size, can happen png files example 'zipped' them selves.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -