我想应用java.util.zip包里的方法实现压缩文件。。但是老板要求压缩时候要可以选用压缩的性能方式。如高压缩率或者最快速度压缩。。我找了半天就找到了个java.util.zip 类 Deflater中一个static int BEST_SPEED 最快压缩的压缩级别 设置。。但是不知道怎么用。 我用zip实现了一个压缩方法。但是设计不到这个类啊。。 查找大量资料说是zlib方式涉及到可以
但是找到的都是压缩字节数组的 帮忙吧大神们。。在线等啦

解决方案 »

  1.   

    public static byte[] getBytesFromFile(File file) throws IOException {
            InputStream is = new FileInputStream(file);
        
            // Get the size of the file
            long length = file.length();
        
            if (length > Integer.MAX_VALUE) {
                // File is too large
            }
        
            // Create the byte array to hold the data
            byte[] bytes = new byte[(int)length];
        
            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length
                   && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
                offset += numRead;
            }
        
            // Ensure all the bytes have been read in
            if (offset < bytes.length) {
                throw new IOException("Could not completely read file "+file.getName());
            }
        
            // Close the input stream and return bytes
            is.close();
            return bytes;
        }
    文件转byte