title: How to reduce the size of an image file without zipping it?
body: <p>Hi i am writing a code in java to compress an image file without zipping it but all that i have been able to do is to zip the file.</p>
  
  <p>This is my code.</p>
  
  <pre><code>package com.test.main;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.zip.ZipEntry;
  import java.util.zip.ZipOutputStream;
  
  public class Main {
  
      public static final double byteSize = 1024;
      public static final String successDir = "E://Tmp/S";
      public static final String failureDir = "E://Tmp/F";
      public static final String tempDir = "E://Tmp/T";
      public static final String delimiter = "/";
      public static final String zipExtn = ".zip";
      public static final String filePath = "E://Tmp/_LND4320HR1.bmp";
  
      public static void main(String[] args) {
          double fileSizeInMB = 0.0;
          File inputFile = null;
          try {
              inputFile = new File(filePath);
              if (inputFile.exists()) {
                  fileSizeInMB = ((inputFile.length() / (byteSize)) / (byteSize));
                  if (fileSizeInMB &lt; 6) {
                      writeToDir(inputFile,successDir);
                      System.out.println("Done");
                  } else {
  
                      //Compress file
                      compressFile(inputFile);
  
                      inputFile =  new File(tempDir + delimiter
                              + getFileName(inputFile.getName())+ zipExtn);
                      if(inputFile.exists()){
                          fileSizeInMB = ((inputFile.length() / byteSize) / byteSize);
                          if (fileSizeInMB &lt; 6) {
                              writeToDir(inputFile,successDir);
                              System.out.println("Done");
                          }else{
                              writeToDir(inputFile,failureDir);
                              System.out.println("Not Done");
                          }
                      }
                  }
              }
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public static String getFileName(String fileName) {
          int indexofDot = fileName.lastIndexOf(".");
          return fileName.substring(0,indexofDot);
      }
  
      public static void writeToDir(File inputFile,String dirPath) throws IOException{
          byte[] buffer = new byte[1024];
          FileOutputStream fos = new FileOutputStream(dirPath + delimiter
                  + getFileName(inputFile.getName()) + zipExtn);
          FileInputStream fis = new FileInputStream(inputFile);
          int length;
  
          while ((length = fis.read(buffer)) &gt; 0) {
              fos.write(buffer, 0, length);
          }
          //Closing operations
          fis.close();
          fos.close();
      }
      public static void compressFile(File inputFile) throws IOException{
          byte[] buffer = new byte[1024];
  
          FileOutputStream fos = new FileOutputStream(tempDir + delimiter
                  + getFileName(inputFile.getName())+ zipExtn);
          ZipOutputStream zos = new ZipOutputStream(fos);
          ZipEntry ze = new ZipEntry(inputFile.getName());
          zos.putNextEntry(ze);
          FileInputStream fis = new FileInputStream(inputFile);
          int length;
          while ((length = fis.read(buffer)) &gt; 0) {
              zos.write(buffer, 0, length);
          }
          //Closing operations
          zos.closeEntry();
          zos.close();
          fis.close();
          fos.close();
      }
  
  
  }
  </code></pre>
  
  <p>The above code only zips the file how to convert this code to compress but not zip the file.</p>