title: Transfer folder in socket - java
body: <p>My goal is to transfer a folder via <code>Socket</code> with the following process:</p>
  
  <ol>
  <li>Zip a folder.</li>
  <li>Transfer it from client to server using <code>Socket</code>.</li>
  <li>Have the server extract the zip file.</li>
  </ol>
  
  <p>It is working on the client side, but on the server side the zip file is received but not extracted. If I manually copy the zip file to the server, it is extracted successfully.</p>
  
  <p>What could be causing the zip file to be received but not extracted? I think the problem is on the transferring process.</p>
  
  <p>Here is the client side code :</p>
  
  <pre><code>new ZipUtils("Folder.zip","Folder").zip();
  
  try {
      Socket socket = new Socket("127.0.0.1", 3232);
  
      final File myFile= new File("Folder.zip");
      byte[] mybytearray = new byte[1024*8];
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      DataInputStream dis = new DataInputStream(bis);
      OutputStream os;
      try{
          os = socket.getOutputStream();
          DataOutputStream dos = new DataOutputStream(os);
          dos.writeUTF(myFile.getName());
          dos.writeLong(mybytearray.length);
          int read;
          while((read = dis.read(mybytearray)) != -1){
              dos.write(mybytearray, 0, read);
          }
      }catch (IOException e) {e.printStackTrace();}
  
      dis.close();
      socket.close();
  }catch (IOException e){}
  </code></pre>
  
  <p>And here is the server side code:</p>
  
  <pre><code>private static Socket socket;
  private static ServerSocket serverSocket;
  private static BufferedInputStream bis;
  private static DataInputStream dis;
  private static DataOutputStream dos;
  private static String RootDir = "D:/Devdroid/";
  
  public static void main(String[] args) {
      try {
          ServerSocket serverSocket = new ServerSocket(3232);
          while(true){
              Socket socket = serverSocket.accept();
              bis = new BufferedInputStream(socket.getInputStream());
              dis = new DataInputStream(bis);
              dos = new DataOutputStream(socket.getOutputStream());
  
              String fileName = null;
              InputStream in;
              int bufferSize=0;
              try {
                  bufferSize=socket.getReceiveBufferSize();
                  in=socket.getInputStream();
                  DataInputStream clientData = new DataInputStream(in);
                  fileName = RootDir+clientData.readUTF();
                  OutputStream output = new FileOutputStream(fileName);
                  byte[] buffer = new byte[bufferSize];
                  int read;
                  while((read = clientData.read(buffer)) != -1){
                      output.write(buffer, 0, read);
                  }
                  output.close();
              }catch(IOException e){e.printStackTrace();}
  
  
              new ZipUtils(fileName,RootDir).unzip();
  
  
              dis.close();
              dos.close();
              socket.close();
          }
      }catch(IOException e){e.printStackTrace();}
  }
  </code></pre>
  
  <p>And here is the <code>ZipUtil.java</code> file:</p>
  
  <pre><code>public class ZipUtils {
      private String _zip;
      private String _path;
  
      public ZipUtils(String zip, String path) {
          _zip = zip;
          _path = path;
      }
  
      public void unzip() {
          dirChecker("");
          try  {
              FileInputStream fin = new FileInputStream(_zip);
              ZipInputStream zin = new ZipInputStream(fin);
              ZipEntry ze = null;
              while ((ze = zin.getNextEntry()) != null) {
                  if(ze.isDirectory()) {
                      dirChecker(ze.getName());
                  } else {
                      FileOutputStream fout = new FileOutputStream(_path + ze.getName());
                      BufferedOutputStream bufout = new BufferedOutputStream(fout);
                      byte[] buffer = new byte[1024];
                      int read = 0;
                      while ((read = zin.read(buffer)) != -1) {
                          bufout.write(buffer, 0, read);
                      }
                      zin.closeEntry();
                      bufout.close();
                      fout.close();
                  }
              }
              zin.close();
          } catch(Exception e) {e.printStackTrace();}
      }
      private void dirChecker(String dir) {
          File f = new File(_path + dir);
          if(!f.isDirectory()) {f.mkdirs();}
      }
  
      public void zip(){
          try{
              FileOutputStream fos = new FileOutputStream(_zip);          
              ZipOutputStream zipOut = new ZipOutputStream(fos);
              File fileToZip = new File(_path);
              try{
                  zipFile(fileToZip, fileToZip.getName(), zipOut);
                  zipOut.close();
                  fos.close();
              }catch(IOException e){}
          }catch(FileNotFoundException e){}
      }
      private void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
          if (fileToZip.isHidden()) {
              return;
          }
          if (fileToZip.isDirectory()) {
              if (fileName.endsWith("/")) {
                  zipOut.putNextEntry(new ZipEntry(fileName));
                  zipOut.closeEntry();
              } else {
                  zipOut.putNextEntry(new ZipEntry(fileName + "/"));
                  zipOut.closeEntry();
              }
              File[] children = fileToZip.listFiles();
              for (File childFile : children) {
                  zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
              }
              return;
          }
          FileInputStream fis = new FileInputStream(fileToZip);
          ZipEntry zipEntry = new ZipEntry(fileName);
          zipOut.putNextEntry(zipEntry);
          byte[] bytes = new byte[1024];
          int length;
          while ((length = fis.read(bytes)) &gt;= 0) {
              zipOut.write(bytes, 0, length);
          }
          fis.close();
      }
  
  }
  </code></pre>