public int createFile( String name, String dir, String content, DatasetBean datasetBean, long time, ExportFormatBean efb, boolean saveToDB) { ArchivedDatasetFileBean fbFinal = new ArchivedDatasetFileBean(); // >> tbh 04/2010 #4915 replace all names' spaces with underscores name = name.replaceAll(" ", "_"); fbFinal.setId(0); try { File complete = new File(dir); if (!complete.isDirectory()) { complete.mkdirs(); } File newFile = new File(complete, name); newFile.setLastModified(System.currentTimeMillis()); BufferedWriter w = new BufferedWriter(new FileWriter(newFile)); w.write(content); w.close(); logger.info("finished writing the text file..."); // now, we write the file to the zip file FileInputStream is = new FileInputStream(newFile); ZipOutputStream z = new ZipOutputStream(new FileOutputStream(new File(complete, name + ".zip"))); logger.info("created zip output stream..."); // we write over the content no matter what // we then check to make sure there are no duplicates // TODO need to change the above -- save all content! // z.write(content); z.putNextEntry(new java.util.zip.ZipEntry(name)); // int length = (int) newFile.length(); int bytesRead; byte[] buff = new byte[512]; // read from buffered input stream and put into zip file // while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { while ((bytesRead = is.read(buff)) != -1) { z.write(buff, 0, bytesRead); } logger.info("writing buffer..."); // } z.closeEntry(); z.finish(); // newFile = new File(complete, name+".zip"); // newFile.setLastModified(System.currentTimeMillis()); // // BufferedWriter w2 = new BufferedWriter(new FileWriter(newFile)); // w2.write(newOut.toString()); // w2.close(); if (is != null) { try { is.close(); } catch (java.io.IOException ie) { ie.printStackTrace(); } } logger.info("finished zipping up file..."); // set up the zip to go into the database if (saveToDB) { ArchivedDatasetFileBean fb = new ArchivedDatasetFileBean(); fb.setName(name + ".zip"); // logger.info("ODM filename: " + name + ".zip"); fb.setFileReference(dir + name + ".zip"); // logger.info("ODM fileReference: " + dir + name + ".zip"); // current location of the file on the system fb.setFileSize((int) newFile.length()); // logger.info("ODM setFileSize: " + (int)newFile.length() ); // set the above to compressed size? fb.setRunTime((int) time); // logger.info("ODM setRunTime: " + (int)time ); // need to set this in milliseconds, get it passed from above // methods? fb.setDatasetId(datasetBean.getId()); // logger.info("ODM setDatasetid: " + ds.getId() ); fb.setExportFormatBean(efb); // logger.info("ODM setExportFormatBean: success" ); fb.setExportFormatId(efb.getId()); // logger.info("ODM setExportFormatId: " + efb.getId()); fb.setOwner(userBean); // logger.info("ODM setOwner: " + sm.getUserBean()); fb.setOwnerId(userBean.getId()); // logger.info("ODM setOwnerId: " + sm.getUserBean().getId() ); fb.setDateCreated(new Date(System.currentTimeMillis())); boolean write = true; ArchivedDatasetFileDAO asdfDAO = new ArchivedDatasetFileDAO(ds); // eliminating all checks so that we create multiple files, tbh 6-7 if (write) { fbFinal = (ArchivedDatasetFileBean) asdfDAO.create(fb); } else { logger.info("duplicate found: " + fb.getName()); } } // created in database! } catch (Exception e) { logger.warn(e.getMessage()); System.out.println("-- exception thrown at createFile: " + e.getMessage()); logger.info("-- exception thrown at createFile: " + e.getMessage()); e.printStackTrace(); } return fbFinal.getId(); }
public int createFile( String zipName, ArrayList names, String dir, ArrayList contents, DatasetBean datasetBean, long time, ExportFormatBean efb, boolean saveToDB) { ArchivedDatasetFileBean fbFinal = new ArchivedDatasetFileBean(); // >> tbh #4915 zipName = zipName.replaceAll(" ", "_"); fbFinal.setId(0); try { File complete = new File(dir); if (!complete.isDirectory()) { complete.mkdirs(); } int totalSize = 0; ZipOutputStream z = new ZipOutputStream(new FileOutputStream(new File(complete, zipName + ".zip"))); FileInputStream is = null; for (int i = 0; i < names.size(); i++) { String name = (String) names.get(i); // >> tbh #4915 name = name.replaceAll(" ", "_"); String content = (String) contents.get(i); File newFile = new File(complete, name); // totalSize = totalSize + (int)newFile.length(); newFile.setLastModified(System.currentTimeMillis()); BufferedWriter w = new BufferedWriter(new FileWriter(newFile)); w.write(content); w.close(); logger.info("finished writing the text file..."); // now, we write the file to the zip file is = new FileInputStream(newFile); logger.info("created zip output stream..."); z.putNextEntry(new java.util.zip.ZipEntry(name)); int bytesRead; byte[] buff = new byte[512]; while ((bytesRead = is.read(buff)) != -1) { z.write(buff, 0, bytesRead); totalSize += 512; } z.closeEntry(); // A. Hamid. 4910 is.close(); if (CoreResources.getField("dataset_file_delete").equalsIgnoreCase("true") || CoreResources.getField("dataset_file_delete").equals("")) { newFile.delete(); } } logger.info("writing buffer..."); // } z.flush(); z.finish(); z.close(); if (is != null) { try { is.close(); } catch (java.io.IOException ie) { ie.printStackTrace(); } } logger.info("finished zipping up file..."); // set up the zip to go into the database if (saveToDB) { ArchivedDatasetFileBean fb = new ArchivedDatasetFileBean(); fb.setName(zipName + ".zip"); fb.setFileReference(dir + zipName + ".zip"); // current location of the file on the system fb.setFileSize(totalSize); // set the above to compressed size? fb.setRunTime((int) time); // need to set this in milliseconds, get it passed from above // methods? fb.setDatasetId(datasetBean.getId()); fb.setExportFormatBean(efb); fb.setExportFormatId(efb.getId()); fb.setOwner(userBean); fb.setOwnerId(userBean.getId()); fb.setDateCreated(new Date(System.currentTimeMillis())); boolean write = true; ArchivedDatasetFileDAO asdfDAO = new ArchivedDatasetFileDAO(ds); if (write) { fbFinal = (ArchivedDatasetFileBean) asdfDAO.create(fb); logger.info("Created ADSFile!: " + fbFinal.getId() + " for " + zipName + ".zip"); } else { logger.info("duplicate found: " + fb.getName()); } } // created in database! } catch (Exception e) { logger.warn(e.getMessage()); System.out.println("-- exception at create file: " + e.getMessage()); e.printStackTrace(); } return fbFinal.getId(); }