0

I am trying to send mail with byte array which is compressed by GZIP, and then rename it with **.csv.gz. I can not open the **.csv file, however, and it shows the file is broken.

I sent it by the following code:

ByteArrayOutputStream obj = new ByteArrayOutputStream();
gzipOS = new GZIPOutputStream(obj);
gzipOS.write(content.getBytes());//content is csv format
byte[] data = obj.toByteArray();
return data;

I tried many mime types, but it does not work:

DataSource dataSource2 = new ByteArrayDataSource(data, "text/plain");  
messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(dataSource2));
messageBodyPart.setFileName("kk.csv.gz");

This attachment is sent successfully, but when I open it with 7-zip, and try to compress it, it shows this file is broken.

Is there any way to slove this problem?

I think this is caused by the formatting when I rename it with **.csv.gz but **.csv.gz is necessary.

Tips: disk is not allowed to save the file, so I send it by this way.

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Wade Chen
  • 31
  • 1
  • 6
  • The fact that you're using text/plain with UTF8 for binary data strikes me as suspicious. There is an answer here that I believe may be related: http://stackoverflow.com/questions/23023583/mimebodypart-getcontent-corrupts-binary-data – Alan Samet Oct 19 '16 at 03:01
  • I had tried many mime types, it does not work. – Wade Chen Oct 19 '16 at 03:10
  • You definitely want to use the right MIME type, e.g., application/octet-stream. Also, be sure to close gzipOS when you're done writing. – Bill Shannon Oct 19 '16 at 03:27
  • After gzipOS.write(...), try calling .finish(). I haven't tried this, but I thought that there might be some data that had not been flushed to the stream. Doing some searching online mentioned a .finish() method. – Alan Samet Oct 19 '16 at 03:28
  • When calling gzip.close not need to call gzip.finish because finish is not closing the stream and write the string, close write and close. – Wade Chen Oct 19 '16 at 03:50
  • Thanks for your kindness , I had finished this issue, that is because I forget to close the stream, I original close the stream by finally{} , so this byte array will return partly before closing it, this make the data miss. – Wade Chen Oct 19 '16 at 04:56

0 Answers0