public static byte[] zipBytes(String filename, byte[] input) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); ZipEntry entry = new ZipEntry(filename); entry.setSize(input.length); zos.putNextEntry(entry); zos.write(input); zos.closeEntry(); zos.close(); return baos.toByteArray(); }
/** * Zips bytes. * * @param input Input bytes. * @return Zipped byte array. * @throws IOException If failed. */ public static byte[] zipBytes(byte[] input) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(DFLT_BUFFER_SIZE); try (ZipOutputStream zos = new ZipOutputStream(baos)) { ZipEntry entry = new ZipEntry(""); try { entry.setSize(input.length); zos.putNextEntry(entry); zos.write(input); } finally { zos.closeEntry(); } } return baos.toByteArray(); }
/** * Zips byte array. * * @param input Input bytes. * @param initBufSize Initial buffer size. * @return Zipped byte array. * @throws IOException If failed. */ public static byte[] zipBytes(byte[] input, int initBufSize) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(initBufSize); try (ZipOutputStream zos = new ZipOutputStream(bos)) { try { ZipEntry entry = new ZipEntry(""); entry.setSize(input.length); zos.putNextEntry(entry); zos.write(input); } finally { zos.closeEntry(); } } return bos.toByteArray(); }
public static void addFolderToZip(ZipOutputStream zos, String path, String comment) throws IOException { while (path.length() != 0 && path.charAt(0) == '/') { path = path.substring(1); } // add folder record if (!StringUtil.endsWithChar(path, '/')) { path += '/'; } ZipEntry zipEntry = new ZipEntry(path); zipEntry.setTime(System.currentTimeMillis()); if (comment != null) { zipEntry.setComment(comment); } zipEntry.setSize(0); zipEntry.setCrc(0); zos.putNextEntry(zipEntry); zos.closeEntry(); }
static public void sendZipFile(ZipOutputStream zos, File file) throws Exception { String zipFilename = file.getAbsolutePath(); int index = zipFilename.lastIndexOf(File.separator); if(index >= 0){ zipFilename = zipFilename.substring(index+1); } ZipEntry zipEntry = new ZipEntry(zipFilename); zipEntry.setMethod(ZipEntry.DEFLATED); zipEntry.setSize(file.length()); zos.putNextEntry(zipEntry); int readSize = 0; byte[] buffer = new byte[FILE_BUFFER_SIZE]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); while (0 != (readSize = bis.read(buffer))) { if(-1 == readSize) break; zos.write(buffer, 0, readSize); } bis.close(); }
static public void sendZipFile(ZipOutputStream zos, File file) throws Exception { String zipFilename = file.getAbsolutePath(); int index = zipFilename.lastIndexOf(File.separator); if(index >= 0){ zipFilename = zipFilename.substring(index+1); } ZipEntry zipEntry = new ZipEntry(zipFilename); zipEntry.setMethod(ZipEntry.DEFLATED); zipEntry.setSize(file.length()); zos.putNextEntry(zipEntry); int readSize = 0; byte[] buffer = new byte[FILE_BUFFER_SIZE]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); while (0 != (readSize = bis.read(buffer))) { if(-1 == readSize) break; zos.write(buffer, 0, readSize); } bis.close(); }
static public void sendZipFile(ZipOutputStream zos, File file) throws Exception { String zipFilename = file.getAbsolutePath(); int index = zipFilename.lastIndexOf(File.separator); if(index >= 0){ zipFilename = zipFilename.substring(index+1); } ZipEntry zipEntry = new ZipEntry(zipFilename); zipEntry.setMethod(ZipEntry.DEFLATED); zipEntry.setSize(file.length()); zos.putNextEntry(zipEntry); int readSize = 0; byte[] buffer = new byte[FILE_BUFFER_SIZE]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); while (0 != (readSize = bis.read(buffer))) { if(-1 == readSize) break; zos.write(buffer, 0, readSize); } bis.close(); }
static public void sendZipFile(ZipOutputStream zos, File file) throws Exception { String zipFilename = file.getAbsolutePath(); int index = zipFilename.lastIndexOf(File.separator); if(index >= 0){ zipFilename = zipFilename.substring(index+1); } ZipEntry zipEntry = new ZipEntry(zipFilename); zipEntry.setMethod(ZipEntry.DEFLATED); zipEntry.setSize(file.length()); zos.putNextEntry(zipEntry); int readSize = 0; byte[] buffer = new byte[FILE_BUFFER_SIZE]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); while (0 != (readSize = bis.read(buffer))) { if(-1 == readSize) break; zos.write(buffer, 0, readSize); } bis.close(); }
static public void sendZipFile(ZipOutputStream zos, File file) throws Exception { String zipFilename = file.getAbsolutePath(); int index = zipFilename.lastIndexOf(File.separator); if(index >= 0){ zipFilename = zipFilename.substring(index+1); } ZipEntry zipEntry = new ZipEntry(zipFilename); zipEntry.setMethod(ZipEntry.DEFLATED); zipEntry.setSize(file.length()); zos.putNextEntry(zipEntry); int readSize = 0; byte[] buffer = new byte[FILE_BUFFER_SIZE]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); while (0 != (readSize = bis.read(buffer))) { if(-1 == readSize) break; zos.write(buffer, 0, readSize); } bis.close(); }
private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<String, String> files) throws BrutException, IOException { File unknownFileDir = new File(appDir, UNK_DIRNAME); // loop through unknown files for (Map.Entry<String,String> unknownFileInfo : files.entrySet()) { File inputFile = new File(unknownFileDir, BrutIO.sanitizeUnknownFile(unknownFileDir, unknownFileInfo.getKey())); if (inputFile.isDirectory()) { continue; } ZipEntry newEntry = new ZipEntry(unknownFileInfo.getKey()); int method = Integer.parseInt(unknownFileInfo.getValue()); LOGGER.fine(String.format("Copying unknown file %s with method %d", unknownFileInfo.getKey(), method)); if (method == ZipEntry.STORED) { newEntry.setMethod(ZipEntry.STORED); newEntry.setSize(inputFile.length()); newEntry.setCompressedSize(-1); BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile)); CRC32 crc = BrutIO.calculateCrc(unknownFile); newEntry.setCrc(crc.getValue()); } else { newEntry.setMethod(ZipEntry.DEFLATED); } outputFile.putNextEntry(newEntry); BrutIO.copy(inputFile, outputFile); outputFile.closeEntry(); } }
if (mDoNotCompress != null && (mDoNotCompress.contains(extension) || mDoNotCompress.contains(zipEntry.getName()))) { zipEntry.setMethod(ZipEntry.STORED); zipEntry.setSize(file.length()); BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(file)); CRC32 crc = BrutIO.calculateCrc(unknownFile);
public static void addFolderToZip(final ZipOutputStream zos, String path, final String comment) throws IOException { while (path.length() != 0 && path.charAt(0) == '/') { path = path.substring(1); } // add folder record if (!StringUtil.endsWithChar(path, '/')) { path += '/'; } ZipEntry zipEntry = new ZipEntry(path); zipEntry.setTime(System.currentTimeMillis()); if (comment != null) { zipEntry.setComment(comment); } zipEntry.setSize(0); zipEntry.setCrc(0); zos.putNextEntry(zipEntry); zos.closeEntry(); }
/** * @param bytes Byte array to compress. * @param compressionLevel Level of compression to encode. * @return Compressed bytes. * @throws IgniteCheckedException If failed. */ public static byte[] zip(@Nullable byte[] bytes, int compressionLevel) throws IgniteCheckedException { try { if (bytes == null) return null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(bos)) { zos.setLevel(compressionLevel); ZipEntry entry = new ZipEntry(""); try { entry.setSize(bytes.length); zos.putNextEntry(entry); zos.write(bytes); } finally { zos.closeEntry(); } } return bos.toByteArray(); } catch (Exception e) { throw new IgniteCheckedException(e); } }
@Override public void closeEntry() throws IOException { ZipEntry delayedEntry = this.delayedEntry; if (delayedEntry != null) { AccessBufByteArrayOutputStream delayedOutputStream = this.delayedOutputStream; byte[] buf = delayedOutputStream.getBuf(); int size = delayedOutputStream.size(); delayedEntry.setSize(size); delayedEntry.setCompressedSize(size); crc.reset(); crc.update(buf, 0, size); delayedEntry.setCrc(crc.getValue()); super.putNextEntry(delayedEntry); super.write(buf, 0, size); this.delayedEntry = null; delayedOutputStream.reset(); } super.closeEntry(); }
private void addFile(ZipOutputStream output, File file, String prefix, boolean compress) throws IOException { //Make sure file exists if (!file.exists()) { return; } ZipEntry entry = new ZipEntry(getEntryName(file, prefix)); entry.setTime(file.lastModified()); entry.setSize(file.length()); if (!compress) { entry.setCrc(calcChecksum(file)); } addToOutputStream(output, Files.newInputStream(file.toPath()), entry); }
crc.update(data); entry = new ZipEntry("resources.arsc"); entry.setSize(data.length); entry.setCrc(crc.getValue()); out.putNextEntry(entry); CRC32 manifestCrc = new CRC32(); manifestCrc.update(manifest); entry.setSize(manifest.length); entry.setCompressedSize(-1); entry.setCrc(manifestCrc.getValue());
zipEntry.setSize(0); zipEntry.setCrc(0);
@Override public void write(Chunk chunk) throws IOException { size += chunk.getSize(); ZipEntry entry = new ZipEntry(StringUtil.toHex(chunk.getChecksum())); entry.setSize(chunk.getSize()); zipOut.putNextEntry(entry); zipOut.write(chunk.getContent(), 0, chunk.getSize()); zipOut.closeEntry(); }
public ZipEntry getEntry() { ZipEntry entry = new ZipEntry(path); if (bytes != null) { entry.setSize(bytes.length); } if(compressionMethod != -1) { entry.setMethod(compressionMethod); } if(crc != -1L) { entry.setCrc(crc); } entry.setTime(time); return entry; }
/** * Create new Zip entry and fill it with associated with file meta-info * * @param name Zip entry name * @param file source File * @return newly created Zip entry */ static ZipEntry fromFile(String name, File file) { ZipEntry zipEntry = new ZipEntry(name); if (!file.isDirectory()) { zipEntry.setSize(file.length()); } zipEntry.setTime(file.lastModified()); ZTFilePermissions permissions = ZTFilePermissionsUtil.getDefaultStategy().getPermissions(file); if (permissions != null) { ZipEntryUtil.setZTFilePermissions(zipEntry, permissions); } return zipEntry; }