Ejemplo n.º 1
0
  private boolean readIndex() {
    if (triedToReadIndex || !usePreindexedCache) {
      return false;
    }

    boolean ret = false;
    synchronized (this) {
      triedToReadIndex = true;
      RandomAccessFile raf = null;
      try {
        File indexFileName = getIndexFile();
        raf = new RandomAccessFile(indexFileName, "r");

        long fileStamp = raf.readLong();
        if (zipFile.lastModified() != fileStamp) {
          ret = false;
        } else {
          directories = new LinkedHashMap<RelativeDirectory, DirectoryEntry>();
          int numDirs = raf.readInt();
          for (int nDirs = 0; nDirs < numDirs; nDirs++) {
            int dirNameBytesLen = raf.readInt();
            byte[] dirNameBytes = new byte[dirNameBytesLen];
            raf.read(dirNameBytes);

            RelativeDirectory dirNameStr = getRelativeDirectory(new String(dirNameBytes, "UTF-8"));
            DirectoryEntry de = new DirectoryEntry(dirNameStr, this);
            de.numEntries = raf.readInt();
            de.writtenOffsetOffset = raf.readLong();
            directories.put(dirNameStr, de);
          }
          ret = true;
          zipFileLastModified = fileStamp;
        }
      } catch (Throwable t) {
        // Do nothing
      } finally {
        if (raf != null) {
          try {
            raf.close();
          } catch (Throwable tt) {
            // Do nothing
          }
        }
      }
      if (ret == true) {
        readFromIndex = true;
      }
    }

    return ret;
  }
Ejemplo n.º 2
0
  private boolean writeIndex() {
    boolean ret = false;
    if (readFromIndex || !usePreindexedCache) {
      return true;
    }

    if (!writeIndex) {
      return true;
    }

    File indexFile = getIndexFile();
    if (indexFile == null) {
      return false;
    }

    RandomAccessFile raf = null;
    long writtenSoFar = 0;
    try {
      raf = new RandomAccessFile(indexFile, "rw");

      raf.writeLong(zipFileLastModified);
      writtenSoFar += 8;

      List<DirectoryEntry> directoriesToWrite = new ArrayList<DirectoryEntry>();
      Map<RelativeDirectory, Long> offsets = new HashMap<RelativeDirectory, Long>();
      raf.writeInt(directories.keySet().size());
      writtenSoFar += 4;

      for (RelativeDirectory dirName : directories.keySet()) {
        DirectoryEntry dirEntry = directories.get(dirName);

        directoriesToWrite.add(dirEntry);

        // Write the dir name bytes
        byte[] dirNameBytes = dirName.getPath().getBytes("UTF-8");
        int dirNameBytesLen = dirNameBytes.length;
        raf.writeInt(dirNameBytesLen);
        writtenSoFar += 4;

        raf.write(dirNameBytes);
        writtenSoFar += dirNameBytesLen;

        // Write the number of files in the dir
        List<Entry> dirEntries = dirEntry.getEntriesAsCollection();
        raf.writeInt(dirEntries.size());
        writtenSoFar += 4;

        offsets.put(dirName, new Long(writtenSoFar));

        // Write the offset of the file's data in the dir
        dirEntry.writtenOffsetOffset = 0L;
        raf.writeLong(0L);
        writtenSoFar += 8;
      }

      for (DirectoryEntry de : directoriesToWrite) {
        // Fix up the offset in the directory table
        long currFP = raf.getFilePointer();

        long offsetOffset = offsets.get(de.dirName).longValue();
        raf.seek(offsetOffset);
        raf.writeLong(writtenSoFar);

        raf.seek(currFP);

        // Now write each of the files in the DirectoryEntry
        List<Entry> list = de.getEntriesAsCollection();
        for (Entry zfie : list) {
          // Write the name bytes
          byte[] zfieNameBytes = zfie.name.getBytes("UTF-8");
          int zfieNameBytesLen = zfieNameBytes.length;
          raf.writeInt(zfieNameBytesLen);
          writtenSoFar += 4;
          raf.write(zfieNameBytes);
          writtenSoFar += zfieNameBytesLen;

          // Write isDir
          raf.writeByte(zfie.isDir ? (byte) 1 : (byte) 0);
          writtenSoFar += 1;

          // Write offset of bytes in the real Jar/Zip file
          raf.writeInt(zfie.offset);
          writtenSoFar += 4;

          // Write size of the file in the real Jar/Zip file
          raf.writeInt(zfie.size);
          writtenSoFar += 4;

          // Write compressed size of the file in the real Jar/Zip file
          raf.writeInt(zfie.compressedSize);
          writtenSoFar += 4;

          // Write java time stamp of the file in the real Jar/Zip file
          raf.writeLong(zfie.getLastModified());
          writtenSoFar += 8;
        }
      }
    } catch (Throwable t) {
      // Do nothing
    } finally {
      try {
        if (raf != null) {
          raf.close();
        }
      } catch (IOException ioe) {
        // Do nothing
      }
    }

    return ret;
  }