private File[] mkTar(FileDTO[] dto, File tarFile, String[] tarEntryNames) throws Exception { File[] srcFiles = new File[dto.length]; try { if (tarFile.getParentFile().mkdirs()) { log.info("M-WRITE " + tarFile.getParent()); } log.info("M-WRITE " + tarFile); TarOutputStream tar = new TarOutputStream(new FileOutputStream(tarFile)); try { writeMD5SUM(tar, dto, tarEntryNames); for (int i = 0; i < tarEntryNames.length; i++) { srcFiles[i] = writeFile(tar, dto[i], tarEntryNames[i]); } } finally { tar.close(); } if (verifyCopy) { VerifyTar.verify(tarFile, new byte[bufferSize]); } } catch (Exception e) { deleteFile(tarFile); throw e; } return srcFiles; }
private File writeFile(TarOutputStream tar, FileDTO dto, String tarEntryName) throws IOException, FileNotFoundException { File file = FileUtils.toFile(dto.getDirectoryPath(), dto.getFilePath()); TarEntry entry = new TarEntry(tarEntryName); entry.setSize(dto.getFileSize()); tar.putNextEntry(entry); FileInputStream fis = new FileInputStream(file); try { tar.copyEntryContents(fis); } finally { fis.close(); } tar.closeEntry(); return file; }
private void writeMD5SUM(TarOutputStream tar, FileDTO[] dto, String[] tarEntryNames) throws IOException { byte[] md5sum = new byte[dto.length * MD5SUM_ENTRY_LEN]; final TarEntry tarEntry = new TarEntry("MD5SUM"); tarEntry.setSize(md5sum.length); tar.putNextEntry(tarEntry); int i = 0; for (int j = 0; j < tarEntryNames.length; j++) { MD5Utils.toHexChars(dto[j].getFileMd5(), md5sum, i); md5sum[i + 32] = ' '; md5sum[i + 33] = ' '; System.arraycopy(tarEntryNames[j].getBytes("US-ASCII"), 0, md5sum, i + 34, 17); md5sum[i + 51] = '\n'; i += MD5SUM_ENTRY_LEN; } tar.write(md5sum); tar.closeEntry(); }