How to extract file back in order ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moroccanplaya
    New Member
    • Jan 2011
    • 80

    #1

    How to extract file back in order ?

    hi i have created a program which creates a file then lets u add other files on to it then it should be able to extract it back out in order. my program can only extract an archive with only one file in it back out but with out the original file name, i hope some one can help me to solve my problems
    heres my code
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    There wasn't any code attached and the description of what it does is a bit vague to me, but it sounds like you're trying to make something akin to the tar command.

    Is this a programming exercise or are you creating a tool for use? Sounds to me like you might be better off just using tar, it's quite handy.

    Comment

    • moroccanplaya
      New Member
      • Jan 2011
      • 80

      #3
      no its an exercise im trying to do other wise i would if of used tar ages ago

      Comment

      • mac11
        Contributor
        • Apr 2007
        • 256

        #4
        You still didn't provide any code, so I'm still just guessing here...

        Does your archive hold the names of the original files?

        Comment

        • moroccanplaya
          New Member
          • Jan 2011
          • 80

          #5
          here is the full code i hope this helps

          Code:
          #include<stdio.h>
          #include<stdlib.h>
          #include<fcntl.h>
          #include<string.h>
          #define QUIT 5 
          
          
          int get_menu_choice ( void ); 
          
          int main()
          {
            
          
              //buffer for the text/binary to be stored//
              unsigned char buffer[10000];
              // to store the number of characters to read/write "for fread/fwrite//
              int n;
              //char to store the user input for system output// 
              char input[4] = "ls"; 
              //int to store the user choice// 
              int choice = 0 ;
              
              //name of new archive is held// 
              char archive_name[256];  
              //name of the original file is held// 
              char original_file[256];
              //name of archive is held//
              char archive[256];
              //name of archive that is going to be extracted is held// 
              char xarchive[256];
              //name of the file that is extracted is held// 
              char extracted[256];
              long size;
              
              //points to the original file that's is to be copied//
              FILE *original_pointer = NULL;
              //points to the copied file//
              FILE *copy_pointer = NULL;
              //points to created  archive//
              FILE *create_pointer = NULL;
              //points to the open file// 
              FILE *open_pointer = NULL;  
              //points to the stored archive// 
              FILE *archive_pointer = NULL;
              //points to the extracted file// 
              FILE *copy = NULL;
              //points to the open extracted file// 
              FILE *open_xtractcp = NULL;
              //points to the archive that's about to be extracted//
              FILE *xtract = NULL ;
              FILE *fileinfo =NULL ;
              
              
           
              
          
              
              
          	        while (choice != QUIT )
             	    {
                          //brings up the menu//	    
                  	 
                  	choice = get_menu_choice();
                  	
                  	if (choice == '1')
                  {
                		while(1) //while true// 
                  {
          		puts("please give your archive a name and a path:\n");
          		scanf("%s", archive_name);
          	
                          //try to open the file for reading,  sees if its able to read it, if its not = to NULL then file already exist //    
                  	if(( create_pointer = fopen(archive_name, "r")) != NULL)
                  {
                  	printf("file already exists\n");
                  	continue;
          		fclose(create_pointer);
                  	
                  }
                      
                     
                          //if file does not already exist then creates the file// 
                 	        create_pointer = fopen( archive_name, "wb");
                 	        fclose(create_pointer);
                  	puts("archive created\n");
                  	
                 
                 
                 	        puts("please select the file you want to be copied into your archive\n");
                          scanf("%s",original_file);
                          //try to open the file for reading,  sees if its able to read it, if its = to NULL then file does not exist // 
                          if((original_pointer = fopen(original_file,"r")) == NULL)
                  {
                  	
                     	puts("file does not exist");
          	        break;
          	}
                    
                  	//opens the file  for appending//                                 
                  	original_pointer = fopen(original_file,"a+b");                            
                  	copy_pointer = fopen( archive_name,"a+b");
                  	
                		/*fread and write have 4 arguments first is the char variable where text 
          		can be stored, the second specifies the size of the chunks of text to 
          		read or write at a time, third argument specifies total numbers of 
          		characters to read or write and 4t argument is a file pointer to the file to work with*/
          			
          			
                          //reads the selected file// 
                  	while (( n=fread(buffer,1,sizeof buffer, original_pointer)) > 0) 
                  {
                  	//writes to the archive// 
                  	fwrite(buffer,1, n , copy_pointer);
              	}
              	
          		
          		fseek(copy_pointer, 0, SEEK_END);
          		size = ftell(copy_pointer);
          		rewind(copy_pointer);
          		printf("File size: %ld\n bytes ", size);
          		
                  
              		if(original_pointer !=NULL)
                  {
                      
                  	fclose(original_pointer);
                  }
          		if(copy_pointer != NULL)
          	{
                  	fclose(copy_pointer);
          	}
                  	
                 	        printf("done..... %s  copied to  %s.\n",original_file,archive_name);
                 	        puts("ctrl + c to quit or continue to create another archive");
              
                
                   	
                  
                      
                
                  	if (original_pointer == NULL)
                  {
                  	puts("unable to open original file\n");
                  }
                  	if (copy_pointer == NULL)
                  {
                  	puts("unable to open copied file");
                          
                  }	
                          puts("ctrl + c to quit or continue to create another archive");
                          continue;
                  
                          
                  }
           
                  }
                
             	        if (choice == '2')
                  {
                   
                  	while(1)
                  {
               
                  	puts("please open your archive that you want to extract\n");
                  	scanf("%s",xarchive);
                
                  	if((  xtract = fopen(xarchive,"r")) == NULL) 
                  {
                  	puts("archive does not exists");
                  	break;
                  }
                  	else
                  	xtract = fopen(xarchive,"a+b");
                
                  	
                 	   	puts("enter the path and/or file name where you want to save your extracted file as");
                 	   	scanf("%s",extracted); 
                 	   	if((open_xtractcp = fopen(extracted,"r")) !=NULL)
                  {
                  	
                     	puts("file exists");
          		break;
          	}
          
                      
                     	open_xtractcp = fopen(xarchive,"a+b");
                     	copy = fopen(extracted,"w+b");
                      
                      
                     	while (( n = fread(buffer,1,sizeof buffer, open_xtractcp)) > 0)
                  {
                     	fwrite(buffer,1, n , copy);
              	}
                  
          	       	if((open_xtractcp !=NULL) && (copy != NULL))
                  {
                  
                  	fclose(open_xtractcp);
                  	fclose(copy);
                  
              
                  	printf("done..... %s  extracted to  %s.\n",xarchive,extracted);
                  	puts("ctrl+c to exit or continue extracting another archive");
                  
                  }           
          
                  	if (open_xtractcp == NULL)
                  {
                  	puts("unable to open  archive\n");
                  }
                  	if (xtract == NULL)
                  {
                  	puts("unable to extract");
              
              
          	}
          		continue;
          	}} 
          
          		if(choice == '3')
          	{
          
          		while(1)
          	{
          		puts("please open your archive\n");
          		scanf("%s",archive);			
          		if((archive_pointer = fopen(archive,"r")) ==NULL)
                 	{ 
            	        puts("archive does not exist");
                 	        break; 
                 	}
          		archive_pointer = fopen( archive, "a+b"); 
          		
          		puts("please select the file you want to be copied into your archive\n");
          		scanf("%s",original_file);
          		if((open_pointer = fopen( original_file, "r"))==NULL)
          	{
          		puts("file does not exists"); 
          		break;
          	} 				
          		
          									 
          	
          		open_pointer = fopen(original_file,"a+b");
          								
          		copy_pointer = fopen( archive,"a+b");
          		
          		
          		
          		while (( n = fread(buffer,1,sizeof buffer, open_pointer)) > 0)
          	{
          		fwrite(buffer,1, n , copy_pointer);
          	}
          		if(open_pointer !=NULL)
          	{
          		
          		fclose(open_pointer);	
          			
          		printf("done..... %s  copied to  %s.\n",original_file,archive);
          			
          		puts("ctrl + c to exit or continue to add to archive ");
          			
          	}		
          		
          		if (copy_pointer != NULL)
          	{
          		fclose(copy_pointer);
          		
          	}
          
          		if (open_pointer == NULL)
          	{
          		puts("unable to open file\n");
          	}
          		if (copy_pointer == NULL)
          	{
          		puts("unable to open copied file");
          			
          	
          	}		
          	        continue;
          	}
          		
          	} 
                    	
            		if(choice == '4')
          	{
          		printf("\ntype 'ls' to see your what's in your current directory \n ");
          		scanf("%s", input);
          		if(!strcmp(input,"ls"))
          	{
          		system(input);
          		return 0; 	
          	}
          		else
          	{
                          puts("only type in ls\n\n");		
          		continue;
          	}
          		
          	}
                  
          	        if (choice == '5')
          	        puts("you chose to quit\n");
          	        return 0;
                  
                  }}
                  
                  
                      
               
          	        int get_menu_choice( void )
                  {
                          char select = 0; 
          		do
                  {
          	        printf("\n");
          
          	        printf("\n1 - create archive\n");
          	        printf("\n2 - extract archive\n");
          	        printf("\n3 - add to archive\n");
          	        printf("\n4 - view what's in your current directory\n"); 
          	        printf("\n5 - Quit");
          	        printf("\n");
          	        printf("\nEnter a selection: " );
          	        scanf("%c", &select); 
                  
                  }       
                          //if select is a number between 1 and 5 it will return select// 
          	        while (select < '1' || select > '5') ;
          	        return select;
                  
                     
                  
                  }

          Comment

          • mac11
            Contributor
            • Apr 2007
            • 256

            #6
            Ok, so I ran the program and it's letting you append the file contents together but like I suspected before it doesn't track the file names or have any way of knowing where one file's content stops and the next begins.

            Here's a simple test I did to show what tar does (which you will have to mimic to some degree):
            Code:
            $ echo "FILE 1 CONTENT" > file1
            $ echo "FILE 2 CONTENT" > file2
            $ tar -cf arch.tar file1 file2
            $ cat arch.tar 
            file10000644000175000017500000000001711515361562011622 0ustar  mclarkemclarkeFILE 1 CONTENT
            file20000644000175000017500000000001711515361567011630 0ustar  mclarkemclarkeFILE 2 CONTENT
            So, tar's archive had a file header that has all sorts of stuff to let it know about the files in the archive and not just their content alone.



            You're archive doesn't have to be exactly what tar makes, but you do need some way to know what each file's name is and where each starts and stops.

            historical note: you've had this suggestion before...
            how-extract-my-file
            Last edited by mac11; Jan 18 '11, 07:12 PM. Reason: added historical note

            Comment

            • moroccanplaya
              New Member
              • Jan 2011
              • 80

              #7
              do you have a simple example ?

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Here's some pseudocode
                Code:
                Unsigned Int intFileSize
                Unsigned Int intFileNameLength
                String strFileName
                File fSourceFile
                File fTargetFile
                Files fSourceFiles
                
                fSourceFiles = getFiles()
                fTargetFile = newFile()
                
                For Each fSourceFile In fSourceFiles
                     intFileSize = fileSize(fSourceFile)
                     strFileName = fSourceFile.FileName
                     intFileNameLength = Length(strFileName)
                     fTargetFile.Write(intFileSize + intFileNameLength + strFileName)
                     fTargetFile.Write(fSourceFile)
                Next

                Comment

                Working...