JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result)); JarEntry entry = new JarEntry(DexFormat.DEX_IN_JAR_NAME); entry.setSize(dex.length); jarOut.putNextEntry(entry); jarOut.write(dex);
public void build(final Path pathToExistingFile) throws Exception { try (final JarOutputStream output = buildPluginJar(pathToExistingFile, this.manifest)) { for (String entryName : new TreeSet<String>(this.entries.keySet())) { final Path pathToRealFile = this.entries.get(entryName); if (pathToRealFile == null) { final JarEntry entry = new JarEntry(entryName + "/"); entry.setMethod(JarEntry.STORED); entry.setSize(0); entry.setCrc(0); output.putNextEntry(entry); output.closeEntry(); } else { final JarEntry entry = new JarEntry(entryName); output.putNextEntry(entry); Files.copy(pathToRealFile, output); output.closeEntry(); } } } }
public static void putNextJarEntry(JarOutputStream modelStream, String name, File file) throws IOException { JarEntry entry = new JarEntry(name); entry.setSize(file.length()); modelStream.putNextEntry(entry); InputStream fileStream = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(fileStream, modelStream); fileStream.close(); }
/** * Add directory entry into outputstream. * @param dirPath * @throws IOException */ public void addDirEntry(String dirPath)throws IOException { try{ JarEntry newEntry = new JarEntry(dirPath); newEntry.setSize(0); addEntry(newEntry, null); }catch(ZipException e){ //ignore duplicate directory entry exceptions. } }
/** * Saves a directory entry to the jar output. */ private void saveDirEntry(String path, Set<String> entries, JarOutputStream jarOut) { if (entries.contains(path)) { return; } try { String entry = ""; for (String dir : Splitter.on('/').omitEmptyStrings().split(path)) { entry += dir + '/'; if (entries.add(entry)) { JarEntry jarEntry = new JarEntry(entry); jarEntry.setMethod(JarOutputStream.STORED); jarEntry.setSize(0L); jarEntry.setCrc(0L); jarOut.putNextEntry(jarEntry); jarOut.closeEntry(); } } } catch (IOException e) { throw Throwables.propagate(e); } }
protected JarEntry createEntry(JarEntry rootEntry, JarEntry template) { if(rootEntry == null) return new JarEntry(template); String name = template.getName().substring(rootEntry.getName().length()); JarEntry entry = new JarEntry(name); entry.setComment(template.getComment()); entry.setSize(template.getSize()); entry.setTime(template.getTime()); return entry; }
public void setupStoredEntry(JarEntry entry) { entry.setSize(this.size); entry.setCompressedSize(this.size); entry.setCrc(this.crc.getValue()); entry.setMethod(ZipEntry.STORED); }
public void setupStoredEntry(JarEntry entry) { entry.setSize(this.size); entry.setCompressedSize(this.size); entry.setCrc(this.crc.getValue()); entry.setMethod(ZipEntry.STORED); }
private void createRuntimeConfigJar(Path dir, Map<String, LocalFile> localFiles, Path stagingDir) throws IOException { LOG.debug("Create and copy {}", Constants.Files.RUNTIME_CONFIG_JAR); // Jar everything under the given directory, which contains different files needed by AM/runnable containers Location location = Locations.toLocation(Files.createTempFile(stagingDir, Constants.Files.RUNTIME_CONFIG_JAR, null)); try ( JarOutputStream jarOutput = new JarOutputStream(location.getOutputStream()); DirectoryStream<Path> stream = Files.newDirectoryStream(dir) ) { for (Path path : stream) { JarEntry jarEntry = new JarEntry(path.getFileName().toString()); BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class); jarEntry.setSize(attrs.size()); jarEntry.setLastAccessTime(attrs.lastAccessTime()); jarEntry.setLastModifiedTime(attrs.lastModifiedTime()); jarOutput.putNextEntry(jarEntry); Files.copy(path, jarOutput); jarOutput.closeEntry(); } } LOG.debug("Done {}", Constants.Files.RUNTIME_CONFIG_JAR); localFiles.put(Constants.Files.RUNTIME_CONFIG_JAR, createLocalFile(Constants.Files.RUNTIME_CONFIG_JAR, location, true)); }
private void createRuntimeConfigJar(Path dir, Map<String, LocalFile> localFiles, Path stagingDir) throws IOException { LOG.debug("Create and copy {}", Constants.Files.RUNTIME_CONFIG_JAR); // Jar everything under the given directory, which contains different files needed by AM/runnable containers Location location = Locations.toLocation(Files.createTempFile(stagingDir, Constants.Files.RUNTIME_CONFIG_JAR, null)); try ( JarOutputStream jarOutput = new JarOutputStream(location.getOutputStream()); DirectoryStream<Path> stream = Files.newDirectoryStream(dir) ) { for (Path path : stream) { JarEntry jarEntry = new JarEntry(path.getFileName().toString()); BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class); jarEntry.setSize(attrs.size()); jarEntry.setLastAccessTime(attrs.lastAccessTime()); jarEntry.setLastModifiedTime(attrs.lastModifiedTime()); jarOutput.putNextEntry(jarEntry); Files.copy(path, jarOutput); jarOutput.closeEntry(); } } LOG.debug("Done {}", Constants.Files.RUNTIME_CONFIG_JAR); localFiles.put(Constants.Files.RUNTIME_CONFIG_JAR, createLocalFile(Constants.Files.RUNTIME_CONFIG_JAR, location, true)); }
public void addZipEntry(ZipEntry zipEntry, ZipFile zipFile, String path) throws IOException { if (fJarPackage.areDirectoryEntriesIncluded()) addDirectories(path); JarEntry newEntry= new JarEntry(path.replace(File.separatorChar, '/')); if (fJarPackage.isCompressed()) newEntry.setMethod(ZipEntry.DEFLATED); // Entry is filled automatically. else { newEntry.setMethod(ZipEntry.STORED); newEntry.setSize(zipEntry.getSize()); newEntry.setCrc(zipEntry.getCrc()); } long lastModified= System.currentTimeMillis(); // Set modification time newEntry.setTime(lastModified); addEntry(newEntry, zipFile.getInputStream(zipEntry)); }
/** * @param string * @param is * @throws IOException */ public void addContents(String entryName, InputStream is) throws IOException { ensureOpen(); if ( entriesSet.contains( entryName ) ){ //Duplicate entry return; } JarEntry entry = new JarEntry( entryName ); entry.setMethod( ZipEntry.DEFLATED ); CRC32 checksumCalculator= new CRC32(); ByteStreamCache bsc = new ByteStreamCache(); entry.setSize( StreamUtil.copyStream( is, bsc.getOutputStream() )); checksumCalculator.update( bsc.toByteArray() ); entry.setCrc(checksumCalculator.getValue()); jarOS.putNextEntry( entry ); //Now that we have the correct size, we can copy it to the jar stream StreamUtil.copyStream( bsc.getInputStream(), jarOS ); entriesSet.add( entryName ); }
/** * @param string * @param is * @throws IOException */ public void addContents(String entryName, InputStream is) throws IOException { ensureOpen(); if ( entriesSet.contains( entryName ) ){ //Duplicate entry return; } JarEntry entry = new JarEntry( entryName ); entry.setMethod( ZipEntry.DEFLATED ); CRC32 checksumCalculator= new CRC32(); ByteStreamCache bsc = new ByteStreamCache(); entry.setSize( StreamUtil.copyStream( is, bsc.getOutputStream() )); checksumCalculator.update( bsc.toByteArray() ); entry.setCrc(checksumCalculator.getValue()); jarOS.putNextEntry( entry ); //Now that we have the correct size, we can copy it to the jar stream StreamUtil.copyStream( bsc.getInputStream(), jarOS ); entriesSet.add( entryName ); }
public void addZipEntry(ZipEntry zipEntry, ZipFile zipFile, String path) throws IOException { if (fJarPackage.areDirectoryEntriesIncluded()) addDirectories(path); JarEntry newEntry= new JarEntry(path.replace(File.separatorChar, '/')); if (fJarPackage.isCompressed()) newEntry.setMethod(ZipEntry.DEFLATED); // Entry is filled automatically. else { newEntry.setMethod(ZipEntry.STORED); newEntry.setSize(zipEntry.getSize()); newEntry.setCrc(zipEntry.getCrc()); } long lastModified= System.currentTimeMillis(); // Set modification time newEntry.setTime(lastModified); addEntry(newEntry, zipFile.getInputStream(zipEntry)); }
public void addZipEntryStream(ZipEntry zipEntry, InputStream is, String path) throws IOException { if (fJarPackage.areDirectoryEntriesIncluded()) addDirectories(path); JarEntry newEntry= new JarEntry(path.replace(File.separatorChar, '/')); if (fJarPackage.isCompressed()) newEntry.setMethod(ZipEntry.DEFLATED); // Entry is filled automatically. else { newEntry.setMethod(ZipEntry.STORED); newEntry.setSize(zipEntry.getSize()); newEntry.setCrc(zipEntry.getCrc()); } long lastModified= System.currentTimeMillis(); // Set modification time newEntry.setTime(lastModified); addEntry(newEntry, is); }
public void addZipEntryStream(ZipEntry zipEntry, InputStream is, String path) throws IOException { if (fJarPackage.areDirectoryEntriesIncluded()) addDirectories(path); JarEntry newEntry= new JarEntry(path.replace(File.separatorChar, '/')); if (fJarPackage.isCompressed()) newEntry.setMethod(ZipEntry.DEFLATED); // Entry is filled automatically. else { newEntry.setMethod(ZipEntry.STORED); newEntry.setSize(zipEntry.getSize()); newEntry.setCrc(zipEntry.getCrc()); } long lastModified= System.currentTimeMillis(); // Set modification time newEntry.setTime(lastModified); addEntry(newEntry, is); }
private static void configureStoredEntry(JarEntry entry, File inputFile) throws IOException { ByteSource byteSource = Files.asByteSource(inputFile); long size = inputFile.length(); entry.setMethod(ZipEntry.STORED); entry.setSize(size); entry.setCompressedSize(size); entry.setCrc(byteSource.hash(Hashing.crc32()).padToLong()); }
private static JarEntry smartClone(JarEntry originalJarEntry) { final JarEntry newJarEntry = new JarEntry(originalJarEntry.getName()); newJarEntry.setComment(originalJarEntry.getComment()); newJarEntry.setExtra(originalJarEntry.getExtra()); newJarEntry.setMethod(originalJarEntry.getMethod()); newJarEntry.setTime(originalJarEntry.getTime()); //Must set size and CRC for STORED entries if (newJarEntry.getMethod() == ZipEntry.STORED) { newJarEntry.setSize(originalJarEntry.getSize()); newJarEntry.setCrc(originalJarEntry.getCrc()); } return newJarEntry; }
@Override public JarEntry transform(JarEntry entry) { JarEntry renamedEntry = new JarEntry(this.namePrefix + entry.getName()); renamedEntry.setTime(entry.getTime()); renamedEntry.setSize(entry.getSize()); renamedEntry.setMethod(entry.getMethod()); if (entry.getComment() != null) { renamedEntry.setComment(entry.getComment()); } renamedEntry.setCompressedSize(entry.getCompressedSize()); renamedEntry.setCrc(entry.getCrc()); //setCreationTimeIfPossible(entry, renamedEntry); if (entry.getExtra() != null) { renamedEntry.setExtra(entry.getExtra()); } return renamedEntry; }
@Override public JarEntry transform(JarEntry entry) { JarEntry renamedEntry = new JarEntry(this.namePrefix + entry.getName()); renamedEntry.setTime(entry.getTime()); renamedEntry.setSize(entry.getSize()); renamedEntry.setMethod(entry.getMethod()); if (entry.getComment() != null) { renamedEntry.setComment(entry.getComment()); } renamedEntry.setCompressedSize(entry.getCompressedSize()); renamedEntry.setCrc(entry.getCrc()); //setCreationTimeIfPossible(entry, renamedEntry); if (entry.getExtra() != null) { renamedEntry.setExtra(entry.getExtra()); } return renamedEntry; }