/** Returns the last modified time of this file. */ @Override protected long doGetLastModifiedTime() throws Exception { if (entry == null) { return 0; } return entry.getModTime().getTime(); }
/** * Closes the current tar entry * * @throws IOException */ protected void closeCurrentEntry() throws IOException { if (currentEntry != null) { if (currentEntry.getSize() > currentFileSize) { throw new IOException( "The current entry[" + currentEntry.getName() + "] of size[" + currentEntry.getSize() + "] has not been fully written."); } currentEntry = null; currentFileSize = 0; pad(); } }
/** * Returns the size of the file content (in bytes). Is only called if {@link #doGetType} returns * {@link FileType#FILE}. */ @Override protected long doGetContentSize() { if (entry == null) { return 0; } return entry.getSize(); }
/** * Put an entry on the output stream. This writes the entry's header record and positions the * output stream for writing the contents of the entry. Once this method is called, the stream is * ready for calls to write() to write the entry's contents. Once the contents are written, * closeEntry() <B>MUST be called to ensure that all buffered data is completely written to the * output stream. * * @param entry The TarEntry to be written to the archive. */ public void putNextEntry(TarEntry entry) throws IOException { StringBuffer name = entry.getHeader().name; // NOTE // This check is not adequate, because the maximum file length that // can be placed into a POSIX (ustar) header depends on the precise // locations of the path elements (slashes) within the file's full // pathname. For this reason, writeEntryHeader() can still throw an // InvalidHeaderException if the file's full pathname will not fit // in the header. if ((entry.isUnixTarFormat() && name.length() > TarHeader.NAMELEN) || (!entry.isUnixTarFormat() && name.length() > (TarHeader.NAMELEN + TarHeader.PREFIXLEN))) { throw new InvalidHeaderException( "file name '" + name + "' is too long ( " + name.length() + " > " + (entry.isUnixTarFormat() ? TarHeader.NAMELEN : (TarHeader.NAMELEN + TarHeader.PREFIXLEN)) + " bytes )"); } entry.writeEntryHeader(this.recordBuf); this.buffer.writeRecord(this.recordBuf); this.currBytes = 0; if (entry.isDirectory()) this.currSize = 0; else this.currSize = entry.getSize(); }
/** * Writes the next tar entry header on the stream * * @param entry * @throws IOException */ public void putNextEntry(TarEntry entry) throws IOException { closeCurrentEntry(); byte[] header = new byte[TarConstants.HEADER_BLOCK]; entry.writeEntryHeader(header); write(header); currentEntry = entry; }
/** Sets the details for this file object. */ protected void setTarEntry(final TarEntry entry) { if (this.entry != null) { return; } if ((entry == null) || (entry.isDirectory())) { type = FileType.FOLDER; } else { type = FileType.FILE; } this.entry = entry; }
/** * Checks if the bytes being written exceed the current entry size. * * @see java.io.FilterOutputStream#write(byte[], int, int) */ @Override public void write(byte[] b, int off, int len) throws IOException { if (currentEntry != null && !currentEntry.isDirectory()) { if (currentEntry.getSize() < currentFileSize + len) { throw new IOException( "The current entry[" + currentEntry.getName() + "] size[" + currentEntry.getSize() + "] is smaller than the bytes[" + (currentFileSize + len) + "] being written."); } } out.write(b, off, len); bytesWritten += len; if (currentEntry != null) { currentFileSize += len; } }
@Override public void write(IContainer container, String destinationPath) throws IOException { if (!resolveLinks && container.isLinked(IResource.DEPTH_INFINITE)) { return; } TarEntry newEntry = new TarEntry(destinationPath); if (container.getLocalTimeStamp() != IResource.NULL_STAMP) { newEntry.setTime(container.getLocalTimeStamp() / 1000); } ResourceAttributes attributes = container.getResourceAttributes(); if (attributes != null && attributes.isExecutable()) { newEntry.setMode(newEntry.getMode() | 0111); } if (attributes != null && attributes.isReadOnly()) { newEntry.setMode(newEntry.getMode() & ~0222); } newEntry.setFileType(TarEntry.DIRECTORY); outputStream.putNextEntry(newEntry); }
/** * Write the contents of the file to the tar archive. * * @param entry * @param contents * @exception java.io.IOException * @exception org.eclipse.core.runtime.CoreException */ private void write(TarEntry entry, IFile contents) throws IOException, CoreException { final URI location = contents.getLocationURI(); if (location == null) { throw new FileNotFoundException(contents.getFullPath().toOSString()); } InputStream contentStream = contents.getContents(false); entry.setSize(EFS.getStore(location).fetchInfo().getLength()); outputStream.putNextEntry(entry); try { int n; byte[] readBuffer = new byte[4096]; while ((n = contentStream.read(readBuffer)) > 0) { outputStream.write(readBuffer, 0, n); } } finally { if (contentStream != null) { contentStream.close(); } } outputStream.closeEntry(); }
/** * Write the passed resource to the current archive. * * @param resource org.eclipse.core.resources.IFile * @param destinationPath java.lang.String * @exception java.io.IOException * @exception org.eclipse.core.runtime.CoreException */ @Override public void write(IFile resource, String destinationPath) throws IOException, CoreException { if (!resolveLinks && resource.isLinked(IResource.DEPTH_INFINITE)) { return; } TarEntry newEntry = new TarEntry(destinationPath); if (resource.getLocalTimeStamp() != IResource.NULL_STAMP) { newEntry.setTime(resource.getLocalTimeStamp() / 1000); } ResourceAttributes attributes = resource.getResourceAttributes(); if (attributes != null && attributes.isExecutable()) { newEntry.setMode(newEntry.getMode() | 0111); } if (attributes != null && attributes.isReadOnly()) { newEntry.setMode(newEntry.getMode() & ~0222); } write(newEntry, resource); }
/** * tar a file * * @param entry the file to tar * @param tOut the output stream * @param vPath the path name of the file to tar * @throws IOException on error */ protected void tarFile(ArchiveEntry entry, TarOutputStream tOut, String vPath) throws ArchiverException, IOException { InputStream fIn = null; // don't add "" to the archive if (vPath.length() <= 0) { return; } if (entry.getResource().isDirectory() && !vPath.endsWith("/")) { vPath += "/"; } if (vPath.startsWith("/") && !options.getPreserveLeadingSlashes()) { int l = vPath.length(); if (l <= 1) { // we would end up adding "" to the archive return; } vPath = vPath.substring(1, l); } int pathLength = vPath.length(); try { final TarEntry te; if (!longFileMode.isGnuMode() && pathLength >= TarConstants.NAMELEN) { int maxPosixPathLen = TarConstants.NAMELEN + TarConstants.POSIX_PREFIXLEN; if (longFileMode.isPosixMode()) { if (pathLength > maxPosixPathLen) { te = new TarEntry(vPath); } else { te = new PosixTarEntry(vPath); } } else if (longFileMode.isPosixWarnMode()) { if (pathLength > maxPosixPathLen) { getLogger() .warn("Entry: " + vPath + " longer than " + maxPosixPathLen + " characters."); if (!longWarningGiven) { getLogger() .warn( "Resulting tar file can only be processed " + "successfully by GNU compatible tar commands"); longWarningGiven = true; } te = new TarEntry(vPath); } else { te = new PosixTarEntry(vPath); } } else if (longFileMode.isOmitMode()) { getLogger().info("Omitting: " + vPath); return; } else if (longFileMode.isWarnMode()) { getLogger() .warn("Entry: " + vPath + " longer than " + TarConstants.NAMELEN + " characters."); if (!longWarningGiven) { getLogger() .warn( "Resulting tar file can only be processed " + "successfully by GNU compatible tar commands"); longWarningGiven = true; } te = new TarEntry(vPath); } else if (longFileMode.isFailMode()) { throw new ArchiverException( "Entry: " + vPath + " longer than " + TarConstants.NAMELEN + " characters."); } else { throw new IllegalStateException("Non gnu mode should never get here?"); } } else { /* Did not touch it, because this would change the following default tar format, however * GNU tar can untar POSIX tar, so I think we should us PosixTarEntry here instead. */ te = new TarEntry(vPath); } long teLastModified = entry.getResource().getLastModified(); te.setModTime( teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE ? System.currentTimeMillis() : teLastModified); if (!entry.getResource().isDirectory()) { final long size = entry.getResource().getSize(); te.setSize(size == PlexusIoResource.UNKNOWN_RESOURCE_SIZE ? 0 : size); te.setMode(entry.getMode()); } else { te.setMode(entry.getMode()); } PlexusIoResourceAttributes attributes = entry.getResourceAttributes(); te.setUserName( (attributes != null && attributes.getUserName() != null) ? attributes.getUserName() : options.getUserName()); te.setGroupName( (attributes != null && attributes.getGroupName() != null) ? attributes.getGroupName() : options.getGroup()); final int userId = (attributes != null && attributes.getUserId() != null) ? attributes.getUserId() : options.getUid(); if (userId > 0) { te.setUserId(userId); } final int groupId = (attributes != null && attributes.getGroupId() != null) ? attributes.getGroupId() : options.getGid(); if (groupId > 0) { te.setGroupId(groupId); } tOut.putNextEntry(te); if (!entry.getResource().isDirectory()) { fIn = entry.getInputStream(); byte[] buffer = new byte[8 * 1024]; int count = 0; do { tOut.write(buffer, 0, count); count = fIn.read(buffer, 0, buffer.length); } while (count != -1); } tOut.closeEntry(); } finally { IOUtil.close(fIn); } }
public boolean extractTar(String asset, String target) { byte buf[] = new byte[1024 * 1024]; InputStream assetStream = null; TarInputStream tis = null; try { assetStream = mAssetManager.open(asset, AssetManager.ACCESS_STREAMING); tis = new TarInputStream( new BufferedInputStream( new GZIPInputStream(new BufferedInputStream(assetStream, 8192)), 8192)); } catch (IOException e) { Log.e("python", "opening up extract tar", e); return false; } while (true) { TarEntry entry = null; try { entry = tis.getNextEntry(); } catch (java.io.IOException e) { Log.e("python", "extracting tar", e); return false; } if (entry == null) { break; } Log.i("python", "extracting " + entry.getName()); if (entry.isDirectory()) { try { new File(target + "/" + entry.getName()).mkdirs(); } catch (SecurityException e) { } ; continue; } OutputStream out = null; String path = target + "/" + entry.getName(); try { out = new BufferedOutputStream(new FileOutputStream(path), 8192); } catch (FileNotFoundException e) { } catch (SecurityException e) { } ; if (out == null) { Log.e("python", "could not open " + path); return false; } try { while (true) { int len = tis.read(buf); if (len == -1) { break; } out.write(buf, 0, len); } out.flush(); out.close(); } catch (java.io.IOException e) { Log.e("python", "extracting zip", e); return false; } } try { tis.close(); assetStream.close(); } catch (IOException e) { // pass } return true; }