public class CompressionTest { public static void main(String[] args) { Compressor compressor = new Compressor(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileInputStream fis=null; File file=null; try { URL uri=CompressionTest.class.getResource("/Files/Video.mp4"); file=new File(uri.getPath()); fis = new FileInputStream(file); } catch ( FileNotFoundException fnfe ) { System.out.println( "Unable to open input file"); } try { byte[] videoBytes = getBytesFromFile(file); System.out.println("CompressionVideoToCompress is: '" +videoBytes + "'"); byte[] bytesCompressed = compressor.compress(videoBytes); System.out.println("bytesCompressed is: '" +bytesCompressed+ "'"); byte[] bytesDecompressed=compressor.decompress(bytesCompressed); System.out.println("bytesDecompressed is: '" +bytesDecompressed+ "'"); FileOutputStream out = new FileOutputStream("A.mp4"); out.write(bytesDecompressed,0,bytesDecompressed.length-1); out.close(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("bytesCompressed is: '"); } } public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.MAX_VALUE. if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] bytes = new byte[1064]; // 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; } } class Compressor { public Compressor() {} public byte[] compress(byte[] bytesToCompress) { Deflater deflater = new Deflater(); deflater.setInput(bytesToCompress); deflater.finish(); byte[] bytesCompressed = new byte[Short.MAX_VALUE]; int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed); byte[] returnValues = new byte[numberOfBytesAfterCompression]; System.arraycopy ( bytesCompressed, 0, returnValues, 0, numberOfBytesAfterCompression ); return returnValues; } public byte[] decompress(byte[] bytesToDecompress) { Inflater inflater = new Inflater(); int numberOfBytesToDecompress = bytesToDecompress.length; inflater.setInput ( bytesToDecompress, 0, numberOfBytesToDecompress ); int compressionFactorMaxLikely = 3; int bufferSizeInBytes = numberOfBytesToDecompress * compressionFactorMaxLikely; byte[] bytesDecompressed = new byte[bufferSizeInBytes]; byte[] returnValues = null; try { int numberOfBytesAfterDecompression = inflater.inflate(bytesDecompressed); returnValues = new byte[numberOfBytesAfterDecompression]; System.arraycopy ( bytesDecompressed, 0, returnValues, 0, numberOfBytesAfterDecompression ); } catch (DataFormatException dfe) { dfe.printStackTrace(); } inflater.end(); return returnValues; } }
Deflater
Inflater
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)