Java Code Examples for java.util.zip.ZipEntry#setCompressedSize()

The following examples show how to use java.util.zip.ZipEntry#setCompressedSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZipGenerator.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public void addMimeTypeFile(String statedPath, String actualPath) throws IOException  {
  //  byte data[] = new byte[BUFFER];
    CRC32 crc = new CRC32();
    
  //  FileInputStream fi = new FileInputStream(actualPath);
  //  BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
    out.setLevel(0);
    ZipEntry entry = new ZipEntry(statedPath);
    entry.setExtra(null);
    names.add(statedPath);
    String contents = "application/epub+zip";
    crc.update(contents.getBytes());
    entry.setCompressedSize(contents.length());
    entry.setSize(contents.length());
    entry.setCrc(crc.getValue());
    entry.setMethod(ZipEntry.STORED);
    out.putNextEntry(entry);
 //   int count;
//    while ((count = origin.read(data, 0, BUFFER)) != -1) {
//      out.write(data, 0, count);
//    }
  //  origin.close();
    out.write(contents.getBytes(),0,contents.length());
    out.setLevel(Deflater.BEST_COMPRESSION);
  }
 
Example 2
Source File: UnoPkg.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Copies the content of the specified binary file to the specified output stream.
 *
 * @param  file    the regular file to copy inside the ZIP file.
 * @param  bundle  the ZIP file where to copy the given regular file.
 */
private static void copy(final File file, final ZipOutputStream bundle) throws IOException {
    final String name = file.getName();
    final ZipEntry entry = new ZipEntry(name);
    if (name.endsWith(".png")) {
        final long size = file.length();
        entry.setMethod(ZipOutputStream.STORED);
        entry.setSize(size);
        entry.setCompressedSize(size);
        entry.setCrc(getCRC32(file));
    }
    bundle.putNextEntry(entry);
    try (InputStream in = new FileInputStream(file)) {
        in.transferTo(bundle);
    }
    bundle.closeEntry();
}
 
Example 3
Source File: AutoSTOREDZipOutputStream.java    From dexdiff with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 4
Source File: ZipOutputFileProvider.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void copyFrom(String filename, InputFileProvider inputFileProvider, String outputFilename)
    throws IOException {
  if (filename.equals(outputFilename)) {
    copyFrom(filename, inputFileProvider, inputFileProvider.getZipEntry(filename));
  } else {
    ZipEntry inputEntry = inputFileProvider.getZipEntry(filename);
    // Clone inputEntry with new name, since there's no ZipEntry.setName()
    ZipEntry result = new ZipEntry(outputFilename);
    result.setTime(inputEntry.getTime());
    result.setCrc(inputEntry.getCrc());
    result.setSize(inputEntry.getSize());
    result.setCompressedSize(inputEntry.getCompressedSize());
    result.setMethod(inputEntry.getMethod());
    copyFrom(filename, inputFileProvider, result);
  }
}
 
Example 5
Source File: JarExporter.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
public void write(ZipFile zip, ZipEntry entry) throws IOException {
    track("A", entry.getName());
    boolean changeCompressionLevel = !compressedLevel && CompressionUtil.ENV_SUPPORTS_COMPRESSION_LEVEL_CHANGE;
    if (changeCompressionLevel) {
        // The entry to be written is assumed to be incompressible
        jOut.setLevel(NO_COMPRESSION);
    }
    exportInfo.update(ExportInfo.Type.ADD, entry.getName());
    ZipEntry copy = new ZipEntry(entry);
    copy.setCompressedSize(-1);
    jOut.putNextEntry(copy);
    if (!entry.isDirectory()) {
        // copy
        try (InputStream in = zip.getInputStream(entry)) {
            IOUtils.copy(in, jOut);
        }
    }
    jOut.closeEntry();
    if (changeCompressionLevel) {
        jOut.setLevel(level);
    }
}
 
Example 6
Source File: SpringBootGenerator.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private ZipEntry createZipEntry(File file, String fullPath) throws IOException {
    ZipEntry entry = new ZipEntry(fullPath);

    byte[] buffer = new byte[8192];
    int bytesRead = -1;
    try (InputStream is = new FileInputStream(file)) {
        CRC32 crc = new CRC32();
        int size = 0;
        while ((bytesRead = is.read(buffer)) != -1) {
            crc.update(buffer, 0, bytesRead);
            size += bytesRead;
        }
        entry.setSize(size);
        entry.setCompressedSize(size);
        entry.setCrc(crc.getValue());
        entry.setMethod(ZipEntry.STORED);
        return entry;
    }
}
 
Example 7
Source File: Androlib.java    From ratel with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 8
Source File: ZipWriter.java    From tool.accelerate.core with Apache License 2.0 5 votes vote down vote up
private void createZipFromMap(ZipOutputStream zos) throws IOException {
    log.log(Level.INFO, "Entering method ProjectConstructor.createZipFromMap()");
    for (Map.Entry<String, byte[]> fileEntry : fileMap.entrySet()) {
        byte[] byteArray = fileEntry.getValue();
        ZipEntry entry = new ZipEntry(fileEntry.getKey());
        entry.setSize(byteArray.length);
        entry.setCompressedSize(-1);
        try {
            zos.putNextEntry(entry);
            zos.write(byteArray);
        } catch (IOException e) {
            throw new IOException(e);
        }
    }
}
 
Example 9
Source File: ZipOutputStreamTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void canWriteContentToStoredZipsInModeThrow() throws IOException {
  String name = "cheese.txt";
  byte[] input = "I like cheese".getBytes(UTF_8);
  File reference = File.createTempFile("reference", ".zip");

  try (CustomZipOutputStream out = ZipOutputStreams.newOutputStream(output, THROW_EXCEPTION);
      ZipOutputStream ref = new ZipOutputStream(new FileOutputStream(reference))) {
    ZipEntry entry = new ZipEntry(name);
    entry.setMethod(ZipEntry.STORED);
    entry.setTime(System.currentTimeMillis());
    entry.setSize(input.length);
    entry.setCompressedSize(input.length);
    entry.setCrc(calcCrc(input));
    out.putNextEntry(entry);
    ref.putNextEntry(entry);
    out.write(input);
    ref.write(input);
  }

  assertEquals(ImmutableList.of(new NameAndContent(name, input)), getExtractedEntries(output));

  // also check against the reference implementation
  byte[] seen = Files.readAllBytes(output);
  byte[] expected = Files.readAllBytes(reference.toPath());
  assertArrayEquals(expected, seen);
}
 
Example 10
Source File: UnregisteredStoredEntry.java    From fastods with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ZipEntry asZipEntry() {
    final ZipEntry zipEntry = new ZipEntry(this.fullPath);
    zipEntry.setSize(this.size);
    zipEntry.setCompressedSize(this.size);
    zipEntry.setCrc(this.crc32);
    zipEntry.setMethod(ZipEntry.STORED);
    return zipEntry;
}
 
Example 11
Source File: NativeUnpack.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void writeEntry(JarOutputStream j, String name,
                        long mtime, long lsize, boolean deflateHint,
                        ByteBuffer data0, ByteBuffer data1) throws IOException {
    int size = (int)lsize;
    if (size != lsize)
        throw new IOException("file too large: "+lsize);

    CRC32 crc32 = _crc32;

    if (_verbose > 1)
        Utils.log.fine("Writing entry: "+name+" size="+size
                         +(deflateHint?" deflated":""));

    if (_buf.length < size) {
        int newSize = size;
        while (newSize < _buf.length) {
            newSize <<= 1;
            if (newSize <= 0) {
                newSize = size;
                break;
            }
        }
        _buf = new byte[newSize];
    }
    assert(_buf.length >= size);

    int fillp = 0;
    if (data0 != null) {
        int size0 = data0.capacity();
        data0.get(_buf, fillp, size0);
        fillp += size0;
    }
    if (data1 != null) {
        int size1 = data1.capacity();
        data1.get(_buf, fillp, size1);
        fillp += size1;
    }
    while (fillp < size) {
        // Fill in rest of data from the stream itself.
        int nr = in.read(_buf, fillp, size - fillp);
        if (nr <= 0)  throw new IOException("EOF at end of archive");
        fillp += nr;
    }

    ZipEntry z = new ZipEntry(name);
    z.setTime(mtime * 1000);

    if (size == 0) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(0);
        z.setCrc(0);
        z.setCompressedSize(0);
    } else if (!deflateHint) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(size);
        z.setCompressedSize(size);
        crc32.reset();
        crc32.update(_buf, 0, size);
        z.setCrc(crc32.getValue());
    } else {
        z.setMethod(Deflater.DEFLATED);
        z.setSize(size);
    }

    j.putNextEntry(z);

    if (size > 0)
        j.write(_buf, 0, size);

    j.closeEntry();
    if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
 
Example 12
Source File: ZipReaderTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Test public void testFileData() throws IOException {
  CRC32 crc = new CRC32();
  try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
    ZipEntry foo = new ZipEntry("foo");
    foo.setComment("foo comment.");
    foo.setMethod(ZipEntry.DEFLATED);
    zout.putNextEntry(foo);
    zout.write("foo".getBytes(UTF_8));
    zout.closeEntry();

    ZipEntry bar = new ZipEntry("bar");
    bar.setComment("bar comment.");
    bar.setMethod(ZipEntry.STORED);
    bar.setSize("bar".length());
    bar.setCompressedSize("bar".length());
    crc.reset();
    crc.update("bar".getBytes(UTF_8));
    bar.setCrc(crc.getValue());
    zout.putNextEntry(bar);
    zout.write("bar".getBytes(UTF_8));
    zout.closeEntry();
  }

  try (ZipReader reader = new ZipReader(test, UTF_8)) {
    ZipFileEntry fooEntry = reader.getEntry("foo");
    InputStream fooIn = reader.getInputStream(fooEntry);
    byte[] fooData = new byte[3];
    fooIn.read(fooData);
    byte[] expectedFooData = "foo".getBytes(UTF_8);
    assertThat(fooData).isEqualTo(expectedFooData);

    assertThat(fooIn.read()).isEqualTo(-1);
    assertThat(fooIn.read(fooData)).isEqualTo(-1);
    assertThat(fooIn.read(fooData, 0, 3)).isEqualTo(-1);

    ZipFileEntry barEntry = reader.getEntry("bar");
    InputStream barIn = reader.getInputStream(barEntry);
    byte[] barData = new byte[3];
    barIn.read(barData);
    byte[] expectedBarData = "bar".getBytes(UTF_8);
    assertThat(barData).isEqualTo(expectedBarData);

    assertThat(barIn.read()).isEqualTo(-1);
    assertThat(barIn.read(barData)).isEqualTo(-1);
    assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);

    thrown.expect(IOException.class);
    thrown.expectMessage("Reset is not supported on this type of stream.");
    barIn.reset();
  }
}
 
Example 13
Source File: WorkspaceEndpoint.java    From tool.accelerate.core with Apache License 2.0 4 votes vote down vote up
@GET
@Path("files")
@Produces("application/zip")
//Swagger annotations
@ApiOperation(value = "Retrieve a zip of the content from a directory in the workspace", httpMethod = "GET", notes = "Get zip containing files from the workspace.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully produced a zip of the required workspace files") })
public Response getFiles(@QueryParam("workspace") String workspaceId, @QueryParam("serviceId") String serviceId, @QueryParam("dir") String dir) throws IOException {
    log.info("GET request for /workspace/files");
    String techWorkspaceDir = StarterUtil.getWorkspaceDir(workspaceId) + "/";
    String filesDir = techWorkspaceDir + "/" + serviceId + "/" + dir;
    File directory = new File(filesDir);
    if(directory.exists() && directory.isDirectory()) {
        IOFileFilter filter;
        if("swagger".equals(serviceId)) {
            filter = FileFilterUtils.notFileFilter(new NameFileFilter(new String[]{"RestApplication.java", "AndroidManifest.xml"}));
        } else {
            filter = FileFilterUtils.trueFileFilter();
        }
        Iterator<File> itr = FileUtils.iterateFilesAndDirs(directory, filter, FileFilterUtils.trueFileFilter());
        StreamingOutput so = (OutputStream os) -> {
            ZipOutputStream zos = new ZipOutputStream(os);
            while(itr.hasNext()) {
                File file = itr.next();
                if(file.isFile()) {
                    byte[] byteArray = FileUtils.readFileToByteArray(file);
                    String path = file.getAbsolutePath().replace('\\', '/');
                    int index = path.indexOf(serviceId + "/" + dir);
                    String relativePath = path.substring(index);
                    ZipEntry entry = new ZipEntry(relativePath);
                    entry.setSize(byteArray.length);
                    entry.setCompressedSize(-1);
                    try {
                        zos.putNextEntry(entry);
                        zos.write(byteArray);
                    } catch (IOException e) {
                        throw new IOException(e);
                    }
                }
            }
            zos.close();
        };
        log.info("Copied files from " + filesDir + " to zip.");
        return Response.ok(so, "application/zip").header("Content-Disposition", "attachment; filename=\"swagger.zip\"").build();
    } else {
        log.severe("File directory doesn't exist : " + filesDir);
        return Response.status(Status.BAD_REQUEST).entity("File directory specified doesn't exist").build();
    }
}
 
Example 14
Source File: ZipReaderTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Test public void testRawFileData() throws IOException {
  CRC32 crc = new CRC32();
  Deflater deflator = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
  try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
    ZipEntry foo = new ZipEntry("foo");
    foo.setComment("foo comment.");
    foo.setMethod(ZipEntry.DEFLATED);
    zout.putNextEntry(foo);
    zout.write("foo".getBytes(UTF_8));
    zout.closeEntry();

    ZipEntry bar = new ZipEntry("bar");
    bar.setComment("bar comment.");
    bar.setMethod(ZipEntry.STORED);
    bar.setSize("bar".length());
    bar.setCompressedSize("bar".length());
    crc.reset();
    crc.update("bar".getBytes(UTF_8));
    bar.setCrc(crc.getValue());
    zout.putNextEntry(bar);
    zout.write("bar".getBytes(UTF_8));
    zout.closeEntry();
  }

  try (ZipReader reader = new ZipReader(test, UTF_8)) {
    ZipFileEntry fooEntry = reader.getEntry("foo");
    InputStream fooIn = reader.getRawInputStream(fooEntry);
    byte[] fooData = new byte[10];
    fooIn.read(fooData);
    byte[] expectedFooData = new byte[10];
    deflator.reset();
    deflator.setInput("foo".getBytes(UTF_8));
    deflator.finish();
    deflator.deflate(expectedFooData);
    assertThat(fooData).isEqualTo(expectedFooData);

    ZipFileEntry barEntry = reader.getEntry("bar");
    InputStream barIn = reader.getRawInputStream(barEntry);
    byte[] barData = new byte[3];
    barIn.read(barData);
    byte[] expectedBarData = "bar".getBytes(UTF_8);
    assertThat(barData).isEqualTo(expectedBarData);

    assertThat(barIn.read()).isEqualTo(-1);
    assertThat(barIn.read(barData)).isEqualTo(-1);
    assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);

    thrown.expect(IOException.class);
    thrown.expectMessage("Reset is not supported on this type of stream.");
    barIn.reset();
  }
}
 
Example 15
Source File: NativeUnpack.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void writeEntry(JarOutputStream j, String name,
                        long mtime, long lsize, boolean deflateHint,
                        ByteBuffer data0, ByteBuffer data1) throws IOException {
    int size = (int)lsize;
    if (size != lsize)
        throw new IOException("file too large: "+lsize);

    CRC32 crc32 = _crc32;

    if (_verbose > 1)
        Utils.log.fine("Writing entry: "+name+" size="+size
                         +(deflateHint?" deflated":""));

    if (_buf.length < size) {
        int newSize = size;
        while (newSize < _buf.length) {
            newSize <<= 1;
            if (newSize <= 0) {
                newSize = size;
                break;
            }
        }
        _buf = new byte[newSize];
    }
    assert(_buf.length >= size);

    int fillp = 0;
    if (data0 != null) {
        int size0 = data0.capacity();
        data0.get(_buf, fillp, size0);
        fillp += size0;
    }
    if (data1 != null) {
        int size1 = data1.capacity();
        data1.get(_buf, fillp, size1);
        fillp += size1;
    }
    while (fillp < size) {
        // Fill in rest of data from the stream itself.
        int nr = in.read(_buf, fillp, size - fillp);
        if (nr <= 0)  throw new IOException("EOF at end of archive");
        fillp += nr;
    }

    ZipEntry z = new ZipEntry(name);
    z.setTime(mtime * 1000);

    if (size == 0) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(0);
        z.setCrc(0);
        z.setCompressedSize(0);
    } else if (!deflateHint) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(size);
        z.setCompressedSize(size);
        crc32.reset();
        crc32.update(_buf, 0, size);
        z.setCrc(crc32.getValue());
    } else {
        z.setMethod(Deflater.DEFLATED);
        z.setSize(size);
    }

    j.putNextEntry(z);

    if (size > 0)
        j.write(_buf, 0, size);

    j.closeEntry();
    if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
 
Example 16
Source File: NativeUnpack.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void writeEntry(JarOutputStream j, String name,
                        long mtime, long lsize, boolean deflateHint,
                        ByteBuffer data0, ByteBuffer data1) throws IOException {
    int size = (int)lsize;
    if (size != lsize)
        throw new IOException("file too large: "+lsize);

    CRC32 crc32 = _crc32;

    if (_verbose > 1)
        Utils.log.fine("Writing entry: "+name+" size="+size
                         +(deflateHint?" deflated":""));

    if (_buf.length < size) {
        int newSize = size;
        while (newSize < _buf.length) {
            newSize <<= 1;
            if (newSize <= 0) {
                newSize = size;
                break;
            }
        }
        _buf = new byte[newSize];
    }
    assert(_buf.length >= size);

    int fillp = 0;
    if (data0 != null) {
        int size0 = data0.capacity();
        data0.get(_buf, fillp, size0);
        fillp += size0;
    }
    if (data1 != null) {
        int size1 = data1.capacity();
        data1.get(_buf, fillp, size1);
        fillp += size1;
    }
    while (fillp < size) {
        // Fill in rest of data from the stream itself.
        int nr = in.read(_buf, fillp, size - fillp);
        if (nr <= 0)  throw new IOException("EOF at end of archive");
        fillp += nr;
    }

    ZipEntry z = new ZipEntry(name);
    z.setTime(mtime * 1000);

    if (size == 0) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(0);
        z.setCrc(0);
        z.setCompressedSize(0);
    } else if (!deflateHint) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(size);
        z.setCompressedSize(size);
        crc32.reset();
        crc32.update(_buf, 0, size);
        z.setCrc(crc32.getValue());
    } else {
        z.setMethod(Deflater.DEFLATED);
        z.setSize(size);
    }

    j.putNextEntry(z);

    if (size > 0)
        j.write(_buf, 0, size);

    j.closeEntry();
    if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
 
Example 17
Source File: AbstractTransformTask.java    From cglib with Apache License 2.0 4 votes vote down vote up
protected void processJarFile(File file) throws Exception {

        if (verbose) {
            log("processing " + file.toURI());
        }
        
        File tempFile = File.createTempFile(file.getName(), null, new File(file
                .getAbsoluteFile().getParent()));
        try{
            
            ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
            try {
                FileOutputStream fout = new FileOutputStream(tempFile);
                try{
                 ZipOutputStream out = new ZipOutputStream(fout);
                                
                    ZipEntry entry;
                    while ((entry = zip.getNextEntry()) != null) {
                        
                        
                        byte bytes[] = getBytes(zip);
                        
                        if (!entry.isDirectory()) {
                            
                            DataInputStream din = new DataInputStream(
                                              new ByteArrayInputStream(bytes)
                                            );
                            
                            if (din.readInt() == CLASS_MAGIC) {
                                
                                bytes = process(bytes);
                                                        
                            } else {
                                if (verbose) {
                                 log("ignoring " + entry.toString());
                                }
                            }
                        }
                       
                        ZipEntry outEntry = new ZipEntry(entry.getName());
                        outEntry.setMethod(entry.getMethod());
                        outEntry.setComment(entry.getComment());
                        outEntry.setSize(bytes.length);
                        
                        
                        if(outEntry.getMethod() == ZipEntry.STORED){
                            CRC32 crc = new CRC32();
                            crc.update(bytes);
                            outEntry.setCrc( crc.getValue() );
                            outEntry.setCompressedSize(bytes.length);
                        }
                        out.putNextEntry(outEntry);
                        out.write(bytes);
                        out.closeEntry();
                        zip.closeEntry();
                        
                    }
                    out.close(); 
                }finally{
                 fout.close();    
                } 
            } finally {
                zip.close();
            }
            
            
            if(file.delete()){
                
                File newFile = new File(tempFile.getAbsolutePath());
                
                if(!newFile.renameTo(file)){
                    throw new IOException("can not rename " + tempFile + " to " + file);   
                }
                
            }else{
                throw new IOException("can not delete " + file);
            }
            
        }finally{
            
            tempFile.delete();
            
        }
        
    }
 
Example 18
Source File: NativeUnpack.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private void writeEntry(JarOutputStream j, String name,
                        long mtime, long lsize, boolean deflateHint,
                        ByteBuffer data0, ByteBuffer data1) throws IOException {
    int size = (int)lsize;
    if (size != lsize)
        throw new IOException("file too large: "+lsize);

    CRC32 crc32 = _crc32;

    if (_verbose > 1)
        Utils.log.fine("Writing entry: "+name+" size="+size
                         +(deflateHint?" deflated":""));

    if (_buf.length < size) {
        int newSize = size;
        while (newSize < _buf.length) {
            newSize <<= 1;
            if (newSize <= 0) {
                newSize = size;
                break;
            }
        }
        _buf = new byte[newSize];
    }
    assert(_buf.length >= size);

    int fillp = 0;
    if (data0 != null) {
        int size0 = data0.capacity();
        data0.get(_buf, fillp, size0);
        fillp += size0;
    }
    if (data1 != null) {
        int size1 = data1.capacity();
        data1.get(_buf, fillp, size1);
        fillp += size1;
    }
    while (fillp < size) {
        // Fill in rest of data from the stream itself.
        int nr = in.read(_buf, fillp, size - fillp);
        if (nr <= 0)  throw new IOException("EOF at end of archive");
        fillp += nr;
    }

    ZipEntry z = new ZipEntry(name);
    z.setTime(mtime * 1000);

    if (size == 0) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(0);
        z.setCrc(0);
        z.setCompressedSize(0);
    } else if (!deflateHint) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(size);
        z.setCompressedSize(size);
        crc32.reset();
        crc32.update(_buf, 0, size);
        z.setCrc(crc32.getValue());
    } else {
        z.setMethod(Deflater.DEFLATED);
        z.setSize(size);
    }

    j.putNextEntry(z);

    if (size > 0)
        j.write(_buf, 0, size);

    j.closeEntry();
    if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
 
Example 19
Source File: NativeUnpack.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void writeEntry(JarOutputStream j, String name,
                        long mtime, long lsize, boolean deflateHint,
                        ByteBuffer data0, ByteBuffer data1) throws IOException {
    int size = (int)lsize;
    if (size != lsize)
        throw new IOException("file too large: "+lsize);

    CRC32 crc32 = _crc32;

    if (_verbose > 1)
        Utils.log.fine("Writing entry: "+name+" size="+size
                         +(deflateHint?" deflated":""));

    if (_buf.length < size) {
        int newSize = size;
        while (newSize < _buf.length) {
            newSize <<= 1;
            if (newSize <= 0) {
                newSize = size;
                break;
            }
        }
        _buf = new byte[newSize];
    }
    assert(_buf.length >= size);

    int fillp = 0;
    if (data0 != null) {
        int size0 = data0.capacity();
        data0.get(_buf, fillp, size0);
        fillp += size0;
    }
    if (data1 != null) {
        int size1 = data1.capacity();
        data1.get(_buf, fillp, size1);
        fillp += size1;
    }
    while (fillp < size) {
        // Fill in rest of data from the stream itself.
        int nr = in.read(_buf, fillp, size - fillp);
        if (nr <= 0)  throw new IOException("EOF at end of archive");
        fillp += nr;
    }

    ZipEntry z = new ZipEntry(name);
    z.setTime(mtime * 1000);

    if (size == 0) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(0);
        z.setCrc(0);
        z.setCompressedSize(0);
    } else if (!deflateHint) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(size);
        z.setCompressedSize(size);
        crc32.reset();
        crc32.update(_buf, 0, size);
        z.setCrc(crc32.getValue());
    } else {
        z.setMethod(Deflater.DEFLATED);
        z.setSize(size);
    }

    j.putNextEntry(z);

    if (size > 0)
        j.write(_buf, 0, size);

    j.closeEntry();
    if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}
 
Example 20
Source File: NativeUnpack.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private void writeEntry(JarOutputStream j, String name,
                        long mtime, long lsize, boolean deflateHint,
                        ByteBuffer data0, ByteBuffer data1) throws IOException {
    int size = (int)lsize;
    if (size != lsize)
        throw new IOException("file too large: "+lsize);

    CRC32 crc32 = _crc32;

    if (_verbose > 1)
        Utils.log.fine("Writing entry: "+name+" size="+size
                         +(deflateHint?" deflated":""));

    if (_buf.length < size) {
        int newSize = size;
        while (newSize < _buf.length) {
            newSize <<= 1;
            if (newSize <= 0) {
                newSize = size;
                break;
            }
        }
        _buf = new byte[newSize];
    }
    assert(_buf.length >= size);

    int fillp = 0;
    if (data0 != null) {
        int size0 = data0.capacity();
        data0.get(_buf, fillp, size0);
        fillp += size0;
    }
    if (data1 != null) {
        int size1 = data1.capacity();
        data1.get(_buf, fillp, size1);
        fillp += size1;
    }
    while (fillp < size) {
        // Fill in rest of data from the stream itself.
        int nr = in.read(_buf, fillp, size - fillp);
        if (nr <= 0)  throw new IOException("EOF at end of archive");
        fillp += nr;
    }

    ZipEntry z = new ZipEntry(name);
    z.setTime(mtime * 1000);

    if (size == 0) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(0);
        z.setCrc(0);
        z.setCompressedSize(0);
    } else if (!deflateHint) {
        z.setMethod(ZipOutputStream.STORED);
        z.setSize(size);
        z.setCompressedSize(size);
        crc32.reset();
        crc32.update(_buf, 0, size);
        z.setCrc(crc32.getValue());
    } else {
        z.setMethod(Deflater.DEFLATED);
        z.setSize(size);
    }

    j.putNextEntry(z);

    if (size > 0)
        j.write(_buf, 0, size);

    j.closeEntry();
    if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
}