Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

How to reduce the size of File?

In my project, I read/write the content of a file with RandomAccessFile.
Sometime, I need to truncate the unused part of the file and reduce the file's size.
I don't want to recreate a new file, because the file will be very big (~200MB)
Is there any method to resize an existing file?

Any idea?
0
threshold
Asked:
threshold
  • 4
  • 4
1 Solution
 
diakovCommented:
Hi,
I'm afraid it is not possible to truncate file from java.
You have to define some abstract operation 'compact', which you will call when you decide that the internal fragmentation of the file is to big. And you have to create another file and delete the first one.
By the way, there are some very interesting algorythms for swap file managemet. For example, there is a way to swap large blocks of data within a file, without increasing it's size or increasing it with the most optimal size.

Regards,
  Nikolay
----------------------------
e-mail: diakov@theoffice.net
0
 
thresholdAuthor Commented:
I said, I am finding a way to reduce the size of a big file without recreating file.
The file is too big, I can't recreate another one again, just to reduce several MB s.
Maybe, there are no way in pure java, the native method that can work is acceptable.
0
 
thresholdAuthor Commented:
btw... My platform is NT 4.0 sp3
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
diakovCommented:
Hi 'threshold',

OK, then, native calls.
For more information on JNI, go to:
http://java.sun.com/products/jdk/1.2/docs/guide/jni/spec/jniTOC.doc.html

Then, what you need is to implement a small shared .dll which implements the methods declared native and generated from 'javah'. Lets say you got the "c:\t.txt", which you want to truncate. You pass it to the method and there you have to open the file with 'GENERIC_WRITE' file access and then use a call to SetEndOfFile function. This function is in 'winbase.h', and is a kernel function. It is supported in NT. It does exactly what you want.

I wrote a small console application that truncates an existing file to 1 byte:

#include "windows.h"
#include "stdio.h"


void main()
{

      HANDLE h;
      
      h = CreateFile("c:\\t.txt",      // pointer to name of the file
    GENERIC_WRITE,      // access (read-write) mode
    0,      // share mode, 0 - cannot be shared
    NULL,      // pointer to security descriptor, not needed
    OPEN_EXISTING,      // how to create
    0,      // file attributes
    NULL      // handle to file with attributes to copy  
   );
      if (h == 0)
      {
            printf("Error openning the file!");
      }
      else
      {
            DWORD pos = SetFilePointer(
          h,      // handle of file
            1,      // number of bytes to move file pointer
            NULL,      // address of high-order word of distance to move  
            FILE_BEGIN       // how to move
            );
            if (pos == 0xFFFFFFFF)
            {
                  printf("Could not set file pos to 1.");
            }
            else
                   if (!SetEndOfFile(h))
                  {
                        printf("Could not truncate file!");
                  };

            CloseHandle(h);
      }
}

Success,
  Nik.

0
 
diakovCommented:
Btw, this question appeared to be good one for section C++ :-)
0
 
thresholdAuthor Commented:
Thank you, diakov...

Maybe, I don't need to use JNI to truncate a file. All I need is a simple .exe file. I can use it in this way below.

c:> filetrancate c:\temp\myDATA.dat 323780000

Then, I can do the trancation in Java with Runtime.exec(...);

Can you modify it for me?
0
 
diakovCommented:
Hi 'threshold',
Not a bad idea. Here is your code.

//------------
#include "windows.h"
#include "stdio.h"

#define SUCCESS 0
#define ERR_INVALID_PARAMS 1
#define ERR_FILE_ERROR 2


int main( int argc, char *argv[])
{

  HANDLE h;
      
      
  //some parameter handling
  if (argc < 3)
  {      
    printf("Usage: filetruncate <full path> <new size>\n");
    printf("Usage: example - filetruncate c:\temp\myDATA.dat 323780000\n");
    return ERR_INVALID_PARAMS;
  }

  printf("Processing parameters: \'%s\', \'%s\' \n", argv[1], argv[2]);

  char *path = argv[1];
  long new_size = atol(argv[2]);
      
      
  h = CreateFile(path,      // pointer to name of the file
    GENERIC_WRITE,      // access (read-write) mode
    0,      // share mode, 0 - cannot be shared
    NULL,      // pointer to security descriptor, not needed
    OPEN_EXISTING,      // how to create
    0,      // file attributes
    NULL      // handle to file with attributes to copy  
   );
  if (h == 0)
  {
    printf("Error openning the file!\n");
    return ERR_FILE_ERROR;
  }
  else
  {
    DWORD pos = SetFilePointer(
    h,      // handle of file
    new_size,      // number of bytes to move file pointer
    NULL,      // address of high-order word of distance to move  
    FILE_BEGIN       // how to move
    );
    if (pos == 0xFFFFFFFF)
    {
      printf("Could not set file pos to 1.\n");
      return ERR_FILE_ERROR;
    }    
    else
      if (!SetEndOfFile(h))
      {
        printf("Could not truncate file!\n");
        return ERR_FILE_ERROR;
      };

    CloseHandle(h);
  }
  return SUCCESS;
}
//---------------------
Cheers,
  Nik
0
 
thresholdAuthor Commented:
Thanks, diakov. It's a big favor.
:-)

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now