rendered paste body//Java IOimport java.io.File;import java.io.FilenameFilter;//Java IO Outimport java.io.OutputStream;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.OutputStreamWriter;//Java IO Inimport java.io.FileInputStream;import java.nio.channels.FileChannel;import java.nio.MappedByteBuffer;import java.text.NumberFormat;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.lang.String;import java.util.Calendar;import java.net.URI;import java.util.zip.*;import java.util.Enumeration;import java.io.IOException;import java.io.FileNotFoundException;public class WriteReport{ private InspectorData[] objInspData; private InspectionData[] objInspecData; private String[] strFileNames; private int intIndex; private String strCurrentValue = ""; private String strWriteDirectory; private String strPathToResourcesDir; WriteReport(InspectorData[] objInspector, InspectionData[] objInspection, String strNewWriteDirectory, String strNewPathToResourcesDir, String[] strNewFileNames) { ////System.out.println("In WriteReport"); //Load passed data into variables objInspData = objInspector; objInspecData = objInspection; strWriteDirectory = strNewWriteDirectory; strPathToResourcesDir = strNewPathToResourcesDir; strFileNames = strNewFileNames; } void writeRelsFile() { OutputStreamWriter objWriter = null; try { File myFile = new File(new URI("file:///home//files//Drives//Internal//document.xml.rels")); OutputStream fos = new BufferedOutputStream (new FileOutputStream (myFile)); objWriter = new OutputStreamWriter(fos); String strFileContents = null; strFileContents = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">"; strFileContents += "<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings\" Target=\"webSettings.xml\" />"; strFileContents += "<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\" />"; strFileContents += "<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\" />"; strFileContents += "<Relationship Id=\"rId4\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme\" Target=\"theme/theme1.xml\" />"; strFileContents += "<Relationship Id=\"rId5\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\" Target=\"fontTable.xml\" />"; strFileContents += "<Relationship Id=\"rId6\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/header.png\" />"; strFileContents += "<Relationship Id=\"rId7\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/fan.jpeg\" />"; for (int i=0; i<strFileNames.length; i++) { strFileContents += "<Relationship Id=\"rId" + (i+8) + "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/" + strFileNames[i].replace(".jpg",".jpeg") + "\" />"; } //strFileContents += "<Relationship Id=\"rId" + strFileNames.length + "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties\" Target=\"docProps/app.xml\" />" //strFileContents += "<Relationship Id=\"rId" + strFileNames.length+1 + "\" Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\" Target=\"docProps/core.xml\" />" //strFileContents += "<Relationship Id=\"rId" + strFileNames.length+2 + "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"word/document.xml\" />" strFileContents += "</Relationships>"; char[] chars = strFileContents.toCharArray(); objWriter.write(chars, 0, chars.length); } catch (java.io.IOException e) { e.printStackTrace(); } catch (java.net.URISyntaxException e) { e.printStackTrace(); } finally { try { objWriter.flush(); objWriter.close(); } catch (java.io.IOException e) { e.printStackTrace(); } } } void write() { ////System.out.println("In write"); String strFileContents = ""; FileInputStream fis = null; try { fis = new FileInputStream(new File(new URI("file://" + strPathToResourcesDir + "Java/Header.xml"))); FileChannel fc = fis.getChannel(); MappedByteBuffer mbf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); byte[] barray = new byte[(int)(fc.size())]; mbf.get(barray); strFileContents = new String(barray); //one big string } catch (java.io.IOException e) { e.printStackTrace(); } catch (java.net.URISyntaxException e) { e.printStackTrace(); } finally { try { fis.close(); } catch (java.io.IOException e) { e.printStackTrace(); } } OutputStream objWriter = null; try { File myFile = new File(new URI("file:///home//files//Drives//Internal//document.xml")); objWriter = new FileOutputStream (myFile); //Header modification currently not needed. **** //This section replaces place holder text & removes/modifiers headers/footers //strFileContents = strFileContents.replaceFirst( //strFileContents.substring(strFileContents.indexOf("<w:wordDocument"),strFileContents.indexOf("xml:space=\"preserve\">")), //"<w:wordDocument xmlns:w=\"http://schemas.microsoft.com/office/word/2003/wordml\" xmlns:st1=\"urn:schemas-microsoft-com:office:smarttags\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:sl=\"http://schemas.microsoft.com/schemaLibrary/2003/core\" xmlns:aml=\"http://schemas.microsoft.com/aml/2001/core\" xmlns:wx=\"http://schemas.microsoft.com/office/word/2003/auxHint\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:dt=\"uuid:C2F41010-65B3-11d1-A29F-00AA00C14882\" w:macrosPresent=\"no\" w:embeddedObjPresent=\"no\" w:ocxPresent=\"no\" xml:space=\"preserve\">"); strFileContents = strFileContents.replaceFirst("</wx:sect></w:body></w:wordDocument>", ""); strFileContents = strFileContents.replaceFirst("Var_serial", objInspData[0].getSerial() ); strFileContents = strFileContents.replaceFirst("Var_customer", objInspData[0].getCustomer() ); strFileContents = strFileContents.replaceFirst("Var_site", objInspData[0].getSite() ); strFileContents = strFileContents.replaceFirst("Var_inspector", objInspData[0].getInspector() ); strFileContents = strFileContents.replaceFirst("Var_model", objInspData[0].getModel() ); strFileContents = strFileContents.replaceFirst("Var_comments", objInspData[0].getComments() ); strFileContents = strFileContents.replaceFirst("Var_unit", objInspData[0].getUnitNumber() ); strFileContents = strFileContents.replaceFirst("Var_frame", objInspData[0].getFrame() ); strFileContents = strFileContents.replaceFirst("Var_series", objInspData[0].getSeries() ); strFileContents = strFileContents.replaceFirst("Var_event", objInspData[0].getEvent() ); strFileContents = strFileContents.replaceFirst("Var_firedhours", objInspData[0].getFiredHours() ); strFileContents = strFileContents.replaceFirst("Var_firedstarts", objInspData[0].getFiredStarts() ); strFileContents = strFileContents.replaceFirst("Var_factored", objInspData[0].getFactoredStarts() ); strFileContents = strFileContents.replaceFirst("Var_trips", objInspData[0].getTrips() ); strFileContents = strFileContents.replaceFirst("Var_cpm", objInspData[0].getCPM() ); strFileContents = strFileContents.replaceFirst("Var_location", objInspData[0].getLocation() ); strFileContents += writeTable(); strFileContents += "<w:sectPr w:rsidR=\"002571D0\" w:rsidSect=\"007925E8\"><w:pgSz w:w=\"12240\" w:h=\"15840\" w:code=\"1\" /><w:pgMar w:top=\"1440\" w:right=\"720\" w:bottom=\"1440\" w:left=\"720\" w:header=\"720\" w:footer=\"720\" w:gutter=\"0\" /><w:cols w:space=\"720\" /><w:docGrid w:linePitch=\"360\" /></w:sectPr></w:body></w:document>"; ////System.out.println(strFileContents); //Writing the file byte[] bytes = strFileContents.getBytes(); ////System.out.println("*************" + chars[chars.length-1]); objWriter.write(bytes); ////System.out.println("The length of the array = " + ((int)strFileNames.length+(int)11)); String[] strNewTest = new String[strFileNames.length+13]; strNewTest[0] = "/" + strWriteDirectory + "/report/[Content_Types].xml"; strNewTest[1] = "/" + strWriteDirectory + "/report/word/fontTable.xml"; strNewTest[2] = "/" + strWriteDirectory + "/report/word/settings.xml"; strNewTest[3] = "/" + strWriteDirectory + "/report/word/styles.xml"; strNewTest[4] = "/" + strWriteDirectory + "/report/word/webSettings.xml"; strNewTest[5] = "/home/files/Drives/Internal/document.xml"; strNewTest[6] = "/" + strWriteDirectory + "/report/_rels/.rels"; strNewTest[7] = "/" + strWriteDirectory + "/report/docProps/app.xml"; strNewTest[8] = "/" + strWriteDirectory + "/report/docProps/core.xml"; strNewTest[9] = "/" + strWriteDirectory + "/report/word/theme/theme1.xml"; strNewTest[10] = "/" + strWriteDirectory + "/report/word/media/header.png"; strNewTest[11] = "/" + strWriteDirectory + "/report/word/media/fan.jpeg"; strNewTest[12] = "/home/files/Drives/Internal/document.xml.rels"; for (int i=0; i<strFileNames.length; i++) { ////System.out.println("strFileNames: " + strFileNames[i]); strNewTest[i+13] = "/" + strWriteDirectory + strFileNames[i]; } CreateZipFile("file://" + strWriteDirectory + generateFilename(), strNewTest); } catch (java.io.IOException e) { e.printStackTrace(); } catch (java.net.URISyntaxException e) { e.printStackTrace(); } finally { try { objWriter.flush(); objWriter.close(); } catch (java.io.IOException e) { e.printStackTrace(); } } } private String writeTable() { String strFileContents = ""; FileInputStream fis = null; try { fis = new FileInputStream(new File(new URI("file://" + strPathToResourcesDir + "Java/Table.xml"))); FileChannel fc = fis.getChannel(); MappedByteBuffer mbf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); byte[] barray = new byte[(int)(fc.size())]; mbf.get(barray); strFileContents = new String(barray); //one big string } catch (java.io.IOException e) { e.printStackTrace(); ////System.out.println("Table.xml failed"); } catch (java.net.URISyntaxException e) { e.printStackTrace(); } finally { try { fis.close(); } catch (java.io.IOException e) { ////System.out.println("Table.xml failed"); e.printStackTrace(); } } String[] strTemp = new String[objInspecData.length]; for (int i=0; i<objInspecData.length; i++) { //Sets each entry in the array with a table template strTemp[i] = strFileContents; } strFileContents = ""; // This erases the 'strFileContents' variable as it still contains a table template for (int i=0; i<objInspecData.length; i++) { ////System.out.println("Current iter: " + i ); //Fill in the place holder text with information from each report strTemp[i] = strTemp[i].replaceFirst("Var_serial", objInspecData[i].getSerial() ); strTemp[i] = strTemp[i].replaceFirst("Var_customer", objInspecData[i].getCustomer() ); strTemp[i] = strTemp[i].replaceFirst("Var_site", objInspecData[i].getSite() ); strTemp[i] = strTemp[i].replaceFirst("Var_ioi", objInspecData[i].getIOI() ); strTemp[i] = strTemp[i].replaceFirst("Var_ioitype", objInspecData[i].getIOIType() ); strTemp[i] = strTemp[i].replaceFirst("Var_equipmentserial", objInspecData[i].getEquipmentSerial() ); strTemp[i] = strTemp[i].replaceFirst("Var_measurement", objInspecData[i].getMeasurementType() ); strTemp[i] = strTemp[i].replaceFirst("Var_comments", objInspecData[i].getComments() ); strTemp[i] = strTemp[i].replaceFirst("Var_section", objInspecData[i].getSection() ); strTemp[i] = strTemp[i].replaceFirst("Var_stage", objInspecData[i].getStage() ); strTemp[i] = strTemp[i].replaceFirst("Var_component", objInspecData[i].getComponent() ); strTemp[i] = strTemp[i].replaceFirst("Var_location", objInspecData[i].getLocation() ); strTemp[i] = strTemp[i].replaceFirst("Var_observation", objInspecData[i].getObservation() ); strTemp[i] = strTemp[i].replaceFirst(strTemp[i].substring(strTemp[i].indexOf("<v:shape id"),strTemp[i].indexOf("</v:shape>")+10), objInspecData[i].getImage() ); //strTemp[i] = strTemp[i].replaceAll("Var_imglink", "wordml://" + i + ".jpg" ); //System.out.println(strTemp[i]); } for (int i=0; i<objInspecData.length; i++) { strFileContents += strTemp[i]; //Combine all the report entries into one string variable } ////System.out.println(strFileContents); return strFileContents; } private String generateFilename() { try { File dir = new File(new URI("file://" + strWriteDirectory)); //Format the date for the filename in yyyy.mm.dd, year/month/day ex: 2007.01.01 SimpleDateFormat objDateTimeFormat = new SimpleDateFormat("yyyy.MM.dd"); Calendar objCalendar = Calendar.getInstance(); final String strDate = objDateTimeFormat.format(objCalendar.getTime()); //Format the number after the date with three digits no matter the size. Ex: 001, 005, 050, 150 etc.. NumberFormat objNumberFormat = new DecimalFormat("000"); FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { //Check to see if any reports have been created on this date return name.startsWith(strDate); } }; String[] strMatchFiles = dir.list(filter); //Add +1 to the total number of reports made on this date to increment up. int intIncrement = strMatchFiles.length + 1; //You can comment/uncommet any of the lines below to change the file extension (doc, docx, xml) docx is the 'correct' file format however .doc works better. String strOutFile = strDate + "-" + objNumberFormat.format(intIncrement) + ".docx"; return strOutFile; } catch (java.net.URISyntaxException e) { e.printStackTrace(); return null; } } public static void CreateZipFile(String zipFileName, String[] ToCompressFiles) { OutputStreamWriter objWriter = null; try { File myFile = new File(new URI(zipFileName)); OutputStream fos = new BufferedOutputStream (new FileOutputStream (myFile)); String[] fileNames = ToCompressFiles; String tempFilename = null; FileInputStream inStream; ZipOutputStream zipOStream = new ZipOutputStream(fos); zipOStream.setLevel ( Deflater.BEST_COMPRESSION ); for (int loop=0;loop < fileNames.length; loop++) { ////System.out.println("Iter: " + loop + " and " + fileNames[loop]); if ( fileNames[loop].lastIndexOf("Internal") != -1 ) { if ( fileNames[loop].lastIndexOf("document.xml") != -1 ) { ////System.out.println("Found document"); tempFilename = fileNames[loop].substring(fileNames[loop].lastIndexOf("Internal")+9); tempFilename = "word/" + tempFilename; } if ( fileNames[loop].lastIndexOf("document.xml.rels") != -1 ) { ////System.out.println("Found doc rels"); tempFilename = fileNames[loop].substring(fileNames[loop].lastIndexOf("Internal")+9); tempFilename = "word/_rels/" + tempFilename; } } if ( fileNames[loop].lastIndexOf("report") != -1 ) { ////System.out.println("Found Report"); tempFilename = fileNames[loop].substring(fileNames[loop].lastIndexOf("report")+7); } if ( fileNames[loop].lastIndexOf(".jpg") != -1 ) { ////System.out.println("Found JPG"); tempFilename = fileNames[loop].substring(fileNames[loop].lastIndexOf("data")+5); tempFilename = "word/media/" + tempFilename.replace(".jpg",".jpeg"); } ////System.out.println(tempFilename); ////System.out.println("Iter: " + loop + " and " + fileNames[loop]); inStream = new FileInputStream(new File(fileNames[loop])); zipOStream.putNextEntry(new ZipEntry(tempFilename)); int i=0; while ((i=inStream.read())!=-1) { zipOStream.write(i); } zipOStream.closeEntry(); inStream.close(); } zipOStream.flush(); zipOStream.close(); } catch (IllegalArgumentException iae) { iae.printStackTrace(); } catch(FileNotFoundException fe) { ////System.out.println("File not found===="+fe); } catch (IOException ioe) { ////System.out.println("IOException===="+ioe); ioe.printStackTrace(); } catch (java.net.URISyntaxException e) { e.printStackTrace(); } }}