hierynomus / smbj

Server Message Block (SMB2, SMB3) implementation in Java
Other
713 stars 180 forks source link

Append data to existing file #624

Closed ashokmurthy13 closed 3 years ago

ashokmurthy13 commented 3 years ago

Hi,

I need to append data to an existing file. Please find the code below I used. The InputStream which I am passing is an excel file.

 Set<AccessMask> accessMask = new HashSet<>();
 accessMask.add(AccessMask.GENERIC_READ);
 accessMask.add(AccessMask.GENERIC_WRITE);

 Set<FileAttributes> fileAttributes = new HashSet<>();
 fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);

 Set<SMB2CreateOptions> createOptions = new HashSet<>();
 createOptions.add(SMB2CreateOptions.FILE_RANDOM_ACCESS);

 File file = share.openFile("PATH", accessMask, fileAttributes, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN_IF, createOptions);

long fileOffset = 0;
 byte[] buffer = new byte[1024*4];
 int length = inputStream.read(buffer);
 while(length != -1){
   fileOffset = share.getFileInformation("PATH").getStandardInformation().getEndOfFile();
   file.write(buffer, fileOffset, 0, length);
 }

When I execute the above code, I can see that the data append to the file, but when I open the file, it says corrupted or want to recover.

What am I doing wrong here? Can someone help me with this?

ashokmurthy13 commented 3 years ago

I tried the below code, but still facing the issue.

 Set<AccessMask> accessMask = new HashSet<>();
 accessMask.add(AccessMask.FILE_WRITE_DATA);

 Set<FileAttributes> fileAttributes = new HashSet<>();
 fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);

 Set<SMB2CreateOptions> createOptions = new HashSet<>();
 createOptions.add(SMB2CreateOptions.FILE_RANDOM_ACCESS);

 File file = share.openFile("PATH", accessMask, fileAttributes, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN_IF, createOptions);

 OutputStream os = file.getOutputStream(true);
 try{
   byte[] buffer = new byte[4096];
   int length;
   while((length = inputStream.read(buffer)) != -1){
         os.write(buffer, 0, length);
    }
 }finally{
   inputStream.close();
   os.close();
   file.close();
 }
ashokmurthy13 commented 3 years ago

@hierynomus Can you please let me know if there is anything that I missed here?

hierynomus commented 3 years ago

I'm pretty sure that you can't just append data to the end of an excel file. This is a specific format that is binary. It's not a log or text file.

hierynomus commented 3 years ago

Append does work, for instance you can have a look at the integration test here:

https://github.com/hierynomus/smbj/blob/bd3d6ad18bb7c31499364db8c1eb7903fe976ebc/src/it/groovy/com/hierynomus/smbj/SMB2FileIntegrationTest.groovy#L257-L329

ashokmurthy13 commented 3 years ago

@hierynomus yes I realized later that, I am writing the data in the same rows(from 0 to N) every time, during append it overwrites the existing data and the final file is corrupted.

Thanks for the reference file. I will look into it and update back on the results.

hierynomus commented 3 years ago

@ashokmurthy13 Can we close this issue?

ashokmurthy13 commented 3 years ago

@hierynomus yes we can close this. thank you.