Search code examples
iosobjective-czipbase64url-encoding

Converting file to Base64 URL safe encoding format in iOS


I am supposed to convert a file in to Base64 and add that String to a JSON which I am appending to a URL. The files that I am having are a .zip file and a text file with .info extension. The .info file is uploading correctly but with .zip file I am getting "Incorrect Padding" error as response from the server.

Below is my work;

- (void)uploadingData: (NSString *)fileName {

    NSArray *directoryPathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [directoryPathsArray objectAtIndex:0];

    NSString *absoluteFilePath = [NSString stringWithFormat:@"%@/%@/%@", documentsDirectory, baseDirName, fileName];

    NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:absoluteFilePath];
    [inputStream open];

    uint8_t buffer[1024];

    int len;

    NSMutableString *total = [[NSMutableString alloc] init];

    while ([inputStream hasBytesAvailable]) {
        len = [inputStream read:buffer maxLength:sizeof(buffer)];

        if (len > 0) {
             [total appendString: [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]];
        }
    }


    NSData *plainData = [total dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [plainData base64EncodedStringWithOptions:0];

// Adding to JSON and upload goes here.
}

Where I have done the mistake?

Additionally, is there a way I can check the converted string is exactly according to Base64, before it append to JSON and upload to server?

Thanks


Solution

  • I have found the mistakes as well as the answer. I do not need to convert the .zip file in to NSInputStream and I haven't applied URL safe methods to the encoded string. Following is the way I have tackled it.

    - (void)uploadingData: (NSString *)fileName {
    
        NSArray *directoryPathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [directoryPathsArray objectAtIndex:0];
    
        NSString *absoluteFilePath = [NSString stringWithFormat:@"%@/%@/%@", documentsDirectory, baseDirName, fileName];
    
        NSData *zipFileData = [NSData dataWithContentsOfFile:absoluteFilePath];
    
        NSString *base64String = [zipFileData base64EncodedStringWithOptions:0];
    
        base64String = [base64String stringByReplacingOccurrencesOfString:@"/"
                                                                withString:@"_"];
    
        base64String = [base64String stringByReplacingOccurrencesOfString:@"+"
                                                               withString:@"-"];
    
    // Adding to JSON and upload goes here.
    }
    

    In Android there is a way to do both Base64 encoding and URL safe formatting with a single function.

    byte[] bytes = Files.toByteArray(new File(sFileName));
    byte[] encoded = Base64.encodeBase64URLSafe(bytes);
    

    I have no idea whether there is a similar kind of easy way of doing it with iOS too.