1 Reply Latest reply: May 2, 2008 8:17 AM by 807601 RSS

    Byte variation while creating tar.gz file

    807601
      public class CreateTarFile 
      {
          
          public CreateTarFile() {
          }
          private static void readTar(InputStream in, String untarDir) throws IOException
           {
                TarInputStream tin = new TarInputStream(in);
                TarEntry tarEntry = tin.getNextEntry();
                boolean test = tarEntry.isDirectory();
                System.out.println("Is Directory: "+test);
                if(new File(untarDir).exists())
                {
                     while (tarEntry != null)
                     {
                          File destPath = new File(untarDir + File.separatorChar + tarEntry.getName());
                          System.out.println("Processing " + destPath.getAbsoluteFile());
                          if(!tarEntry.isDirectory())
                          {
                               FileOutputStream fout = new FileOutputStream(destPath);
                               tin.copyEntryContents(fout);
                               fout.close();
                          }
                          else
                          {
                               System.out.println("directory does not exist");
                               destPath.mkdir();
                          }
                          tarEntry = tin.getNextEntry();
                     }
                     tin.close();
                }
                else
                {
                     System.out.println("That destination directory doesn't exist! " + untarDir);
                }
           }
           /**
           * Add a file to a Tar OutputStream
           */
           private static boolean addToTar(TarOutputStream tos, File inFile)
           {
                try 
                {
                     //System.out.println("Debug: " + inFile.toString());
                     if ( inFile.exists() && inFile.isFile())
                     {
                          // Create a file input stream and a buffered input stream.
                          FileInputStream fis = new FileInputStream(inFile);
                          BufferedInputStream bis = new BufferedInputStream(fis);
                          
                          // Create a Tar Entry and put it into the archive (no data yet).
                          String filePath = inFile.getPath();
                          System.out.println("filePath: "+filePath);
                          filePath = filePath.substring(filePath.indexOf(File.separatorChar)+1,filePath.length());
                          filePath = filePath.substring(filePath.indexOf(File.separatorChar)+1,filePath.length());
                          filePath = filePath.substring(filePath.indexOf(File.separatorChar)+1,filePath.length());
                          System.out.println("filePath: "+filePath);
                          // TarEntry fileEntry = new TarEntry((File) inFile);
                          TarEntry fileEntry = new TarEntry(filePath);
                          fileEntry.setSize(inFile.length());
                          tos.putNextEntry(fileEntry);
                          
                          // Create a byte array object named data and declare byte count variable.
                          byte[] data = new byte[1024];
                          int byteCount;
           
                          // Create a loop that reads from the buffered input stream and writes
                          // to the tar output stream until the bis has been entirely read.
                          while ((byteCount = bis.read(data)) > -1)
                          {
                               tos.write(data, 0, byteCount);
                          } 
                          tos.closeEntry();
                          
                          return true;
                     }
                     return false;
                }
                catch (IOException exception)
                {
                     exception.printStackTrace();
                     return false;
                }
           }
           
           public static void main(String[] args) 
           {     
                try 
                {
                     TarOutputStream tos = new TarOutputStream(new FileOutputStream("D:/TempFolder/Test1/abc.tar.gz"));
                     File sdir = new File("D:/TempFolder/Test/");
                     File inFile = null;
                     File [] listFiles = sdir.listFiles();
                     for(int i = 0; i < listFiles.length; i++)
                     {
                          inFile = listFiles;
                          if(inFile.isFile())
                          {
                               addToTar(tos, inFile);
                          }
                          else
                          {
                               File [] subListFiles = inFile.listFiles();
                               for(int j = 0; j < subListFiles.length; j++)
                               {
                                    inFile = subListFiles[j];
                                    addToTar(tos, inFile);
                               }
                          }
                     }
                     readTar(new FileInputStream("D:/TempFolder/Test1/abc.tar.gz"), "D:/TempFolder/Test");               
                }
                catch (FileNotFoundException e)
                {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
                catch (IOException e)
                {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
           }
      }


      Inside D:/TempFolder/Test/, I have a folder and some files. The bytes of the manually created abc.tar.gz is 4,532 KB. But when I run my above source code. The created abc.tar.gz is 8,570 KB. And I am also getting the following error
      java.io.IOException: request to write '1024' bytes exceeds size in header of '983040' bytes for entry '\TempFolder\Test1\abc.tar.gz'
              at org.apache.tools.tar.TarOutputStream.write(TarOutputStream.java:279)
      Could anyone please tell me where am I wrong.