/** * Compress the given file with the system encoding. * * @param logFile file to be compressed * @return compressed file byte array */ public static byte[] compress(File logFile) { return compress(new File[]{logFile}, Charset.defaultCharset(), Charset.defaultCharset()); }
/** * Extracting the given byte array into the given file. * * @param zipEntry byte array of compressed file * @param toFile file to be written */ public static void decompress(byte[] zipEntry, File toFile) { FileOutputStream fos = null; ByteArrayInputStream bio = new ByteArrayInputStream(zipEntry); try { fos = new FileOutputStream(toFile); decompress(bio, fos, Long.MAX_VALUE); } catch (IOException e) { LOGGER.error("Error occurs during extracting to {} : {} ", toFile.getAbsolutePath(), e.getMessage()); LOGGER.debug("Details : ", e); } finally { IOUtils.closeQuietly(fos); IOUtils.closeQuietly(bio); } }
@Test public void testLogCompressDecompress() throws IOException { File file = new File(LogCompressUtilTest.class.getResource("/grinder1.properties").getFile()); byte[] zippedContent = LogCompressUtils.compress(file); File createTempFile2 = File.createTempFile("a22aa", ".zip"); createTempFile2.deleteOnExit(); FileUtils.writeByteArrayToFile(createTempFile2, zippedContent); File createTempFile = File.createTempFile("a22", "tmp"); LogCompressUtils.decompress(zippedContent, createTempFile); assertThat(createTempFile.exists(), is(true)); byte[] unzippedContent = FileUtils.readFileToByteArray(createTempFile); assertThat(unzippedContent, is(FileUtils.readFileToByteArray(file))); }
/** * Compress the given file with the system encoding. * * @param logFiles files to be compressed * @return compressed file byte array */ public static byte[] compress(File[] logFiles) { return compress(logFiles, Charset.defaultCharset(), Charset.defaultCharset()); }
private void sendLog(ConsoleCommunication consoleCommunication, String testId) { File logFolder = new File(agentConfig.getHome().getLogDirectory(), testId); if (!logFolder.exists()) { return; } File[] logFiles = logFolder.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return (name.endsWith(".log")); } }); if (logFiles == null || ArrayUtils.isEmpty(logFiles)) { LOGGER.error("No log exists under {}", logFolder.getAbsolutePath()); return; } Arrays.sort(logFiles); // Take only one file... if agent.send.all.logs is not set. if (!agentConfig.getAgentProperties().getPropertyBoolean(PROP_AGENT_ALL_LOGS)) { logFiles = new File[]{logFiles[0]}; } final byte[] compressedLog = LogCompressUtils.compress(logFiles, Charset.defaultCharset(), Charset.forName("UTF-8") ); consoleCommunication.sendMessage(new LogReportGrinderMessage(testId, compressedLog, new AgentAddress(m_agentIdentity))); // Delete logs to clean up if (!agentConfig.getAgentProperties().getPropertyBoolean(PROP_AGENT_KEEP_LOGS)) { LOGGER.info("Clean up the perftest logs"); FileUtils.deleteQuietly(logFolder); } }