Class ZFile
- java.lang.Object
-
- com.android.tools.build.apkzlib.zip.ZFile
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class ZFile extends java.lang.Object implements java.io.CloseableTheZFileprovides the main interface for interacting with zip files. AZFilecan be created on a new file or in an existing file. Once created, files can be added or removed from the zip file.Changes in the zip file are always deferred. Any change requested is made in memory and written to disk only when
update()orclose()is invoked.Zip files are open initially in read-only mode and will switch to read-write when needed. This is done automatically. Because modifications to the file are done in-memory, the zip file can be manipulated when closed. When invoking
update()orclose()the zip file will be reopen and changes will be written. However, the zip file cannot be modified outside the control ofZFile. So, if aZFileis closed, modified outside and then a file is added or removed from the zip file, when reopening the zip file,ZFilewill detect the outside modification and will fail.In memory manipulation means that files added to the zip file are kept in memory until written to disk. This provides much faster operation and allows better zip file allocation (see below). It may, however, increase the memory footprint of the application. When adding large files, if memory consumption is a concern, a call to
update()will actually write the file to disk and discard the memory buffer. Information about allocation can be obtained from aByteTrackerthat can be given to the file on creation.ZFilekeeps track of allocation inside of the zip file. If a file is deleted, its space is marked as freed and will be reused for an added file if it fits in the space. Allocation of files to empty areas is done using a best fit algorithm. When adding a file, if it doesn't fit in any free area, the zip file will be extended.ZFileprovides a fast way to merge data from another zip file (seemergeFrom(ZFile, Predicate)) avoiding recompression and copying of equal files. When merging, patterns of files may be provided that are ignored. This allows handling special files in the merging process, such as files inMETA-INF.When adding files to the zip file, unless files are explicitly required to be stored, files will be deflated. However, deflating will not occur if the deflated file is larger then the stored file, e.g. if compression would yield a bigger file. See
Compressorfor details on how compression works.Because
ZFilewas designed to be used in a build system and not as general-purpose zip utility, it is very strict (and unforgiving) about the zip format and unsupported features.ZFilesupports alignment. Alignment means that file data (not entries -- the local header must be discounted) must start at offsets that are multiple of a number -- the alignment. Alignment is defined by an alignment rules (AlignmentRulein theZFileOptionsobject used to create theZFile.When a file is added to the zip, the alignment rules will be checked and alignment will be honored when positioning the file in the zip. This means that unused spaces in the zip may be generated as a result. However, alignment of existing entries will not be changed.
Entries can be realigned individually (see
StoredEntry.realign()or the full zip file may be realigned (seerealign()). When realigning the full zip entries that are already aligned will not be affected.Because realignment may cause files to move in the zip, realignment is done in-memory meaning that files that need to change location will moved to memory and will only be flushed when either
update()orclose()are called.Alignment only applies to filed that are forced to be uncompressed. This is because alignment is used to allow mapping files in the archive directly into memory and compressing defeats the purpose of alignment.
Manipulating zip files with
ZFilemay yield zip files with empty spaces between files. This happens in two situations: (1) if alignment is required, files may be shifted to conform to the request alignment leaving an empty space before the previous file, and (2) if a file is removed or replaced with a file that does not fit the space it was in. By default,ZFiledoes not do any special processing in these situations. Files are indexed by their offsets from the central directory and empty spaces can exist in the zip file.However, it is possible to tell
ZFileto use the extra field in the local header to do cover the empty spaces. This is done by settingZFileOptions.setCoverEmptySpaceUsingExtraField(boolean)totrue. This has the advantage of leaving no gaps between entries in the zip, as required by some tools like Oracle's {code jar} tool. However, setting this option will destroy the contents of the file's extra field.Activating
ZFileOptions.setCoverEmptySpaceUsingExtraField(boolean)may lead to virtual files being added to the zip file. Since extra field is limited to 64k, it is not possible to cover any space bigger than that using the extra field. In those cases, virtual files are added to the file. A virtual file is a file that exists in the actual zip data, but is not referenced from the central directory. A zip-compliant utility should ignore these files. However, zip utilities that expect the zip to be a stream, such as Oracle's jar, will find these files instead of considering the zip to be corrupt.ZFilesupport sorting zip files. Sorting (done through thesortZipContents()method) is a process by which all files are re-read into memory, if not already in memory, removed from the zip and re-added in alphabetical order, respecting alignment rules. So, in general, filebwill come after fileaunless fileais subject to alignment that forces an empty space before that can be occupied byb. Sorting can be used to minimize the changes between two zips.Sorting in
ZFilecan be done manually or automatically. Manual sorting is done by invokingsortZipContents(). Automatic sorting is done by setting theZFileOptions.getAutoSortFiles()option when creating theZFile. Automatic sorting invokessortZipContents()immediately when doing anupdate()after all extensions have processed theZFileExtension.beforeUpdate(). This has the guarantee that files added by extensions will be sorted, something that does not happen if the invocation is sequential, i.e.,sortZipContents()called beforeupdate(). The drawback of automatic sorting is that sorting will happen every timeupdate()is called and the file is dirty having a possible penalty in performance.To allow whole-apk signing, the
ZFileallows the central directory location to be offset by a fixed amount. This amount can be set using thesetExtraDirectoryOffset(long)method. Setting a non-zero value will add extra (unused) space in the zip file before the central directory. This value can be changed at any time and it will force the central directory rewritten when the file is updated or closed.ZFileprovides an extension mechanism to allow objects to register with the file and be notified when changes to the file happen. This should be used to add extra features to the zip file while providing strong decoupling. SeeZFileExtension,addZFileExtension(ZFileExtension)andremoveZFileExtension(ZFileExtension).This class is not thread-safe. Neither are any of the classes associated with it in this package, except when otherwise noticed.
-
-
Field Summary
Fields Modifier and Type Field Description static charSEPARATORThe file separator in paths in the zip file.
-
Constructor Summary
Constructors Constructor Description ZFile(java.io.File file)Deprecated.ZFile(java.io.File file, ZFileOptions options)Deprecated.ZFile(java.io.File file, ZFileOptions options, boolean readOnly)Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(java.lang.String name, com.google.common.io.ByteSource source, boolean mayCompress)Adds a file to the archive.voidadd(java.lang.String name, java.io.InputStream stream)voidadd(java.lang.String name, java.io.InputStream stream, boolean mayCompress)Adds a file to the archive.voidaddAllRecursively(java.io.File file)Adds all files and directories recursively.voidaddAllRecursively(java.io.File file, com.google.common.base.Predicate<? super java.io.File> mayCompress)Adds all files and directories recursively.voidaddZFileExtension(ZFileExtension extension)Adds an extension to this zip file.booleanareTimestampsIgnored()Obtains whether thisZFileis ignoring timestamps.com.android.apksig.util.DataSourceasDataSource()com.android.apksig.util.DataSourceasDataSource(long offset, long size)voidclose()Updates the file and closes it.voiddirectFullyRead(long offset, byte[] data)Reads exactlydata.lengthbytes of data, failing if it was not possible to read all the requested data.voiddirectFullyRead(long offset, java.nio.ByteBuffer dest)Reads exactlydest.remaining()bytes of data, failing if it was not possible to read all the requested data.java.io.InputStreamdirectOpen(long start, long end)Opens a portion of the zip for reading.intdirectRead(long offset, byte[] data)Same asdirectRead(offset, data, 0, data.length).intdirectRead(long offset, byte[] data, int start, int count)Directly reads data from the zip file.intdirectRead(long offset, java.nio.ByteBuffer dest)Directly reads data from the zip file.longdirectSize()Returns the current size (in bytes) of the underlying file.voiddirectWrite(long offset, byte[] data)Same asdirectWrite(offset, data, 0, data.length).voiddirectWrite(long offset, byte[] data, int start, int count)Directly writes data in the zip file.java.util.Set<StoredEntry>entries()Obtains all entries in the file.voidfinishAllBackgroundTasks()Wait for any background tasks to finish and report any errors.StoredEntryget(java.lang.String path)Obtains an entry at a given path in the zip.byte[]getCentralDirectoryBytes()Obtains the byte array representation of the central directory.longgetCentralDirectoryOffset()Obtains the offset at which the central directory exists, or at which it will be written if the zip file were to be flushed immediately.longgetCentralDirectorySize()Obtains the size of the central directory, if the central directory is written in the zip file.byte[]getEocdBytes()Obtains the byte array representation of the EOCD.byte[]getEocdComment()Obtains the comment in the EOCD.longgetEocdOffset()Obtains the offset of the EOCD record, if the EOCD has been written to the file.longgetEocdSize()Obtains the size of the EOCD record, if the EOCD has been written to the file.longgetExtraDirectoryOffset()Obtains the extra offset for the central directory.java.io.FilegetFile()Obtains the filesystem path to the zip file.booleangetSkipValidation()ByteStoragegetStorage()Obtains the storage used by the zip to store data.booleanhasPendingChangesWithWait()Are there in-memory changes that have not been written to the zip file?voidmergeFrom(ZFile src, com.google.common.base.Predicate<java.lang.String> ignoreFilter)Adds all files from another zip file, maintaining their compression.voidopenReadOnly()Deprecated.useopenReadOnlyIfClosed()if necessary to ensure aZFileis open and readablestatic ZFileopenReadOnly(java.io.File file)Opens a newZFilefrom the given file in read-only mode.static ZFileopenReadOnly(java.io.File file, ZFileOptions options)Opens a newZFilefrom the given file in read-only mode.voidopenReadOnlyIfClosed()If the zip file is closed, opens it in read-only mode.static ZFileopenReadWrite(java.io.File file)Opens a newZFilefrom the given file in read-write mode.static ZFileopenReadWrite(java.io.File file, ZFileOptions options)Opens a newZFilefrom the given file in read-write mode.booleanrealign()Realigns all entries in the zip.voidremoveZFileExtension(ZFileExtension extension)Removes an extension from this zip file.voidsetEocdComment(byte[] comment)Sets the comment in the EOCD.voidsetExtraDirectoryOffset(long offset)Sets an extra offset for the central directory.voidsortZipContents()Sorts all files in the zip.voidtouch()voidupdate()Updates the file writing new entries and removing deleted entries.
-
-
-
Field Detail
-
SEPARATOR
public static final char SEPARATOR
The file separator in paths in the zip file. This is fixed by the zip specification (section 4.4.17).- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ZFile
@Deprecated public ZFile(java.io.File file) throws java.io.IOExceptionDeprecated.Creates a new zip file. If the zip file does not exist, then no file is created at this point andZFilewill contain an empty structure. However, an (empty) zip file will be created if eitherupdate()orclose()are used. If a zip file exists, it will be parsed and read.- Parameters:
file- the zip file- Throws:
java.io.IOException- some file exists but could not be read
-
ZFile
@Deprecated public ZFile(java.io.File file, ZFileOptions options) throws java.io.IOExceptionDeprecated.Creates a new zip file. If the zip file does not exist, then no file is created at this point andZFilewill contain an empty structure. However, an (empty) zip file will be created if eitherupdate()orclose()are used. If a zip file exists, it will be parsed and read.- Parameters:
file- the zip fileoptions- configuration options- Throws:
java.io.IOException- some file exists but could not be read
-
ZFile
@Deprecated public ZFile(java.io.File file, ZFileOptions options, boolean readOnly) throws java.io.IOExceptionDeprecated.Creates a new zip file. If the zip file does not exist, then no file is created at this point andZFilewill contain an empty structure. However, an (empty) zip file will be created if eitherupdate()orclose()are used. If a zip file exists, it will be parsed and read.- Parameters:
file- the zip fileoptions- configuration optionsreadOnly- should the file be open in read-only mode? Iftruethen the file must exist and no methods can be invoked that could potentially change the file- Throws:
java.io.IOException- some file exists but could not be read
-
-
Method Detail
-
openReadOnly
@Deprecated public void openReadOnly() throws java.io.IOExceptionDeprecated.useopenReadOnlyIfClosed()if necessary to ensure aZFileis open and readableOld name ofopenReadOnlyIfClosed(), method kept for backwards compatibility only.- Throws:
java.io.IOException
-
openReadOnly
public static ZFile openReadOnly(java.io.File file) throws java.io.IOException
Opens a newZFilefrom the given file in read-only mode.- Parameters:
file- the file to open- Returns:
- the created file
- Throws:
java.io.IOException- failed to read the file
-
openReadOnly
public static ZFile openReadOnly(java.io.File file, ZFileOptions options) throws java.io.IOException
Opens a newZFilefrom the given file in read-only mode.- Parameters:
file- the file to openoptions- the options to use to open the file; because the file is open read-only, many of these options won't have any effect- Returns:
- the created file
- Throws:
java.io.IOException- failed to read the file
-
openReadWrite
public static ZFile openReadWrite(java.io.File file) throws java.io.IOException
Opens a newZFilefrom the given file in read-write mode. Opening a file in read-write mode may force the file to be written even if no changes are made. For example, differences in signature will force the file to be written. UseopenReadOnly(File, ZFileOptions)to open a file and ensure it won't be written.The file will be created if it doesn't exist. If the file exists, it must be a valid zip archive.
- Parameters:
file- the file to open- Returns:
- the created file
- Throws:
java.io.IOException- failed to read the file
-
openReadWrite
public static ZFile openReadWrite(java.io.File file, ZFileOptions options) throws java.io.IOException
Opens a newZFilefrom the given file in read-write mode. Opening a file in read-write mode may force the file to be written even if no changes are made. For example, differences in signature will force the file to be written. UseopenReadOnly(File, ZFileOptions)to open a file and ensure it won't be written.The file will be created if it doesn't exist. If the file exists, it must be a valid zip archive.
- Parameters:
file- the file to openoptions- the options to use to open the file- Returns:
- the created file
- Throws:
java.io.IOException- failed to read the file
-
getSkipValidation
public boolean getSkipValidation()
-
entries
public java.util.Set<StoredEntry> entries()
Obtains all entries in the file. Entries themselves may be or not written in disk. However, all of them can be open for reading.- Returns:
- all entries in the zip
-
get
@Nullable public StoredEntry get(java.lang.String path)
Obtains an entry at a given path in the zip.- Parameters:
path- the path- Returns:
- the entry at the path or
nullif none exists
-
directOpen
public java.io.InputStream directOpen(long start, long end) throws java.io.IOExceptionOpens a portion of the zip for reading. The zip must be open for this method to be invoked. Note that if the zip has not been updated, the individual zip entries may not have been written yet.- Parameters:
start- the index within the zip file to start readingend- the index within the zip file to end reading (the actual byte pointed by end will not be read)- Returns:
- a stream that will read the portion of the file; no decompression is done, data is returned as is
- Throws:
java.io.IOException- failed to open the zip file
-
update
public void update() throws java.io.IOExceptionUpdates the file writing new entries and removing deleted entries. This will force reopening the file as read/write if the file wasn't open in read/write mode.- Throws:
java.io.IOException- failed to update the file; this exception may have been thrown by the compressor but only reported here
-
close
public void close() throws java.io.IOExceptionUpdates the file and closes it.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
getCentralDirectoryBytes
public byte[] getCentralDirectoryBytes() throws java.io.IOExceptionObtains the byte array representation of the central directory. The central directory must have been already computed. If there are no entries in the zip, the central directory will be empty.- Returns:
- the byte representation, or an empty array if there are no entries in the zip
- Throws:
java.io.IOException- failed to compute the central directory byte representation
-
getEocdBytes
public byte[] getEocdBytes() throws java.io.IOExceptionObtains the byte array representation of the EOCD. The EOCD must have already been computed for this method to be invoked.- Returns:
- the byte representation of the EOCD
- Throws:
java.io.IOException- failed to obtain the byte representation of the EOCD
-
openReadOnlyIfClosed
public void openReadOnlyIfClosed() throws java.io.IOExceptionIf the zip file is closed, opens it in read-only mode. If it is already open, does nothing. In general, it is not necessary to directly invoke this method. However, if directly reading the zip file using, for exampledirectRead(long, byte[]), then this method needs to be called.- Throws:
java.io.IOException- failed to open the file
-
add
public void add(java.lang.String name, java.io.InputStream stream) throws java.io.IOException- Parameters:
name- the file name (i.e., path); paths should be defined using slashes and the name should not end in slashstream- the source for the file's data- Throws:
java.io.IOException- failed to read the source datajava.lang.IllegalStateException- if the file is in read-only mode
-
add
public void add(java.lang.String name, java.io.InputStream stream, boolean mayCompress) throws java.io.IOExceptionAdds a file to the archive.Adding the file will not update the archive immediately. Updating will only happen when the
update()method is invoked.Adding a file with the same name as an existing file will replace that file in the archive.
- Parameters:
name- the file name (i.e., path); paths should be defined using slashes and the name should not end in slashstream- the source for the file's datamayCompress- can the file be compressed? This flag will be ignored if the alignment rules force the file to be aligned, in which case the file will not be compressed.- Throws:
java.io.IOException- failed to read the source datajava.lang.IllegalStateException- if the file is in read-only mode
-
add
public void add(java.lang.String name, com.google.common.io.ByteSource source, boolean mayCompress) throws java.io.IOExceptionAdds a file to the archive.Adding the file will not update the archive immediately. Updating will only happen when the
update()method is invoked.Adding a file with the same name as an existing file will replace that file in the archive.
- Parameters:
name- the file name (i.e., path); paths should be defined using slashes and the name should not end in slashsource- the source for the file's datamayCompress- can the file be compressed? This flag will be ignored if the alignment rules force the file to be aligned, in which case the file will not be compressed.- Throws:
java.io.IOException- failed to read the source datajava.lang.IllegalStateException- if the file is in read-only mode
-
mergeFrom
public void mergeFrom(ZFile src, com.google.common.base.Predicate<java.lang.String> ignoreFilter) throws java.io.IOException
Adds all files from another zip file, maintaining their compression. Files specified in src that are already on this file will replace the ones in this file. However, if their sizes and checksums are equal, they will be ignored.This method will not perform any changes in itself, it will only update in-memory data structures. To actually write the zip file, invoke either
update()orclose().- Parameters:
src- the source archiveignoreFilter- predicate that, iftrue, identifies files in src that should be ignored by merging; merging will behave as if these files were not there- Throws:
java.io.IOException- failed to read from src or write on the outputjava.lang.IllegalStateException- if the file is in read-only mode
-
touch
public void touch()
Forcibly marks this zip file as touched, forcing it to be updated whenupdate()orclose()are invoked.- Throws:
java.lang.IllegalStateException- if the file is in read-only mode
-
finishAllBackgroundTasks
public void finishAllBackgroundTasks() throws java.io.IOExceptionWait for any background tasks to finish and report any errors. In general this method does not need to be invoked directly as errors from background tasks are reported duringadd(String, InputStream, boolean),update()andclose(). However, if required for some purposes, e.g., ensuring all notifications have been done to extensions, then this method may be called. It will wait for all background tasks to complete.- Throws:
java.io.IOException- some background work failed
-
realign
public boolean realign() throws java.io.IOExceptionRealigns all entries in the zip. This is equivalent to callStoredEntry.realign()for all entries in the zip file.- Returns:
- has any entry been changed? Note that for entries that have not yet been written on the
file, realignment does not count as a change as nothing needs to be updated in the file;
entries that have been updated may have been recreated and the existing references outside
of
ZFilemay refer toStoredEntrys that are no longer valid - Throws:
java.io.IOException- failed to realign the zip; some entries in the zip may have been lost due to the I/O errorjava.lang.IllegalStateException- if the file is in read-only mode
-
addZFileExtension
public void addZFileExtension(ZFileExtension extension)
Adds an extension to this zip file.- Parameters:
extension- the listener to add- Throws:
java.lang.IllegalStateException- if the file is in read-only mode
-
removeZFileExtension
public void removeZFileExtension(ZFileExtension extension)
Removes an extension from this zip file.- Parameters:
extension- the listener to remove- Throws:
java.lang.IllegalStateException- if the file is in read-only mode
-
directWrite
public void directWrite(long offset, byte[] data, int start, int count) throws java.io.IOExceptionDirectly writes data in the zip file. Incorrect use of this method may corrupt the zip file. Invoking this method may force the zip to be reopened in read/write mode.- Parameters:
offset- the offset at which data should be writtendata- the data to write, may be an empty arraystart- start offset indatawhere data to write is locatedcount- number of bytes of data to write- Throws:
java.io.IOException- failed to write the datajava.lang.IllegalStateException- if the file is in read-only mode
-
directWrite
public void directWrite(long offset, byte[] data) throws java.io.IOExceptionSame asdirectWrite(offset, data, 0, data.length).- Parameters:
offset- the offset at which data should be writtendata- the data to write, may be an empty array- Throws:
java.io.IOException- failed to write the datajava.lang.IllegalStateException- if the file is in read-only mode
-
directSize
public long directSize() throws java.io.IOExceptionReturns the current size (in bytes) of the underlying file.- Throws:
java.io.IOException- if an I/O error occurs
-
directRead
public int directRead(long offset, byte[] data, int start, int count) throws java.io.IOExceptionDirectly reads data from the zip file. Invoking this method may force the zip to be reopened in read/write mode.- Parameters:
offset- the offset at which data should be writtendata- the array where read data should be storedstart- start position in the array where to write data tocount- how many bytes of data can be written- Returns:
- how many bytes of data have been written or
-1if there are no more bytes to be read - Throws:
java.io.IOException- failed to write the data
-
directRead
public int directRead(long offset, java.nio.ByteBuffer dest) throws java.io.IOExceptionDirectly reads data from the zip file. Invoking this method may force the zip to be reopened in read/write mode.- Parameters:
offset- the offset from which data should be readdest- the output buffer to fill with data from theoffset.- Returns:
- how many bytes of data have been written or
-1if there are no more bytes to be read - Throws:
java.io.IOException- failed to write the data
-
directRead
public int directRead(long offset, byte[] data) throws java.io.IOExceptionSame asdirectRead(offset, data, 0, data.length).- Parameters:
offset- the offset at which data should be readdata- receives the read data, may be an empty array- Throws:
java.io.IOException- failed to read the data
-
directFullyRead
public void directFullyRead(long offset, byte[] data) throws java.io.IOExceptionReads exactlydata.lengthbytes of data, failing if it was not possible to read all the requested data.- Parameters:
offset- the offset at which to start readingdata- the array that receives the data read- Throws:
java.io.IOException- failed to read some data or there is not enough data to read
-
directFullyRead
public void directFullyRead(long offset, java.nio.ByteBuffer dest) throws java.io.IOExceptionReads exactlydest.remaining()bytes of data, failing if it was not possible to read all the requested data.- Parameters:
offset- the offset at which to start readingdest- the output buffer to fill with data- Throws:
java.io.IOException- failed to read some data or there is not enough data to read
-
addAllRecursively
public void addAllRecursively(java.io.File file) throws java.io.IOExceptionAdds all files and directories recursively.Equivalent to calling
addAllRecursively(File, Predicate)using a predicate that always returnstrue- Parameters:
file- a file or directory; if it is a directory, all files and directories will be added recursively- Throws:
java.io.IOException- failed to some (or all ) of the filesjava.lang.IllegalStateException- if the file is in read-only mode
-
addAllRecursively
public void addAllRecursively(java.io.File file, com.google.common.base.Predicate<? super java.io.File> mayCompress) throws java.io.IOExceptionAdds all files and directories recursively.- Parameters:
file- a file or directory; if it is a directory, all files and directories will be added recursivelymayCompress- a function that decides whether files may be compressed- Throws:
java.io.IOException- failed to some (or all ) of the filesjava.lang.IllegalStateException- if the file is in read-only mode
-
getCentralDirectoryOffset
public long getCentralDirectoryOffset()
Obtains the offset at which the central directory exists, or at which it will be written if the zip file were to be flushed immediately.- Returns:
- the offset, in bytes, where the central directory is or will be written; this value includes any extra offset for the central directory
-
getCentralDirectorySize
public long getCentralDirectorySize()
Obtains the size of the central directory, if the central directory is written in the zip file.- Returns:
- the size of the central directory or
-1if the central directory has not been computed
-
getEocdOffset
public long getEocdOffset()
Obtains the offset of the EOCD record, if the EOCD has been written to the file.- Returns:
- the offset of the EOCD or
-1if none exists yet
-
getEocdSize
public long getEocdSize()
Obtains the size of the EOCD record, if the EOCD has been written to the file.- Returns:
- the size of the EOCD of
-1it none exists yet
-
getEocdComment
public byte[] getEocdComment()
Obtains the comment in the EOCD.- Returns:
- the comment exactly as it was encoded in the EOCD, no encoding conversion is done
-
setEocdComment
public void setEocdComment(byte[] comment)
Sets the comment in the EOCD.- Parameters:
comment- the new comment; no conversion is done, these exact bytes will be placed in the EOCD comment- Throws:
java.lang.IllegalStateException- if file is in read-only mode
-
setExtraDirectoryOffset
public void setExtraDirectoryOffset(long offset)
Sets an extra offset for the central directory. See class description for details. Changing this value will mark the file as dirty and force a rewrite of the central directory when updated.- Parameters:
offset- the offset or0to write the central directory at its current location- Throws:
java.lang.IllegalStateException- if file is in read-only mode
-
getExtraDirectoryOffset
public long getExtraDirectoryOffset()
Obtains the extra offset for the central directory. See class description for details.- Returns:
- the offset or
0if no offset is set
-
areTimestampsIgnored
public boolean areTimestampsIgnored()
Obtains whether thisZFileis ignoring timestamps.- Returns:
- are the timestamps being ignored?
-
sortZipContents
public void sortZipContents() throws java.io.IOExceptionSorts all files in the zip. This will force all files to be loaded and will wait for all background tasks to complete. Sorting files is never done implicitly and will operate in memory only (maybe reading files from the zip disk into memory, if needed). It will leave the zip in dirty state, requiring a call toupdate()to force the entries to be written to disk.- Throws:
java.io.IOException- failed to load or move a file in the zipjava.lang.IllegalStateException- if file is in read-only mode
-
getFile
public java.io.File getFile()
Obtains the filesystem path to the zip file.- Returns:
- the file that may or may not exist (depending on whether something existed there before the zip was created and on whether the zip has been updated or not)
-
asDataSource
public com.android.apksig.util.DataSource asDataSource() throws java.io.IOException- Throws:
java.io.IOException
-
asDataSource
public com.android.apksig.util.DataSource asDataSource(long offset, long size) throws java.io.IOException- Throws:
java.io.IOException
-
hasPendingChangesWithWait
public boolean hasPendingChangesWithWait() throws java.io.IOExceptionAre there in-memory changes that have not been written to the zip file?Waits for all pending processing which may make changes.
- Throws:
java.io.IOException
-
getStorage
public ByteStorage getStorage()
Obtains the storage used by the zip to store data.- Returns:
- the storage object that should only be used to query data; using this storage for any purposes other than statistics may have undefined results
-
-