0

I have used below java code to unzip a .gz file. The .gz file contains a folder/textFile. It is working fine only if there is some data in the text file.I need it to work even if there is no data in text file. Help me with any modifications in the below java code.

public static void main(String[] args) {
    String sourceZipFile = "C:/SKKIKRFULL20151014.gz";
    final String destinationDir = "C:/";
    RandomAccessFile randomAccessFile = null;
    ISevenZipInArchive inArchive = null;
    try {
        randomAccessFile = new RandomAccessFile(sourceZipFile, "r");
        inArchive = SevenZip.openInArchive(null, // autodetect archive type
                new RandomAccessFileInStream(randomAccessFile));

        // Getting simple interface of the archive inArchive
        ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

        for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {

            final int[] hash = new int[] { 0 };

            if (!item.isFolder()) {

                ExtractOperationResult result;
                result = item.extractSlow(new ISequentialOutStream() {

                    public int write(final byte[] data) throws SevenZipException {
                        try {

                            if (item.getPath().indexOf(File.separator) > 0) {

                                String path = destinationDir + File.separator
                                        + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));

                                File folderExisting = new File(path);

                                if (!folderExisting.exists()) {

                                    new File(path).mkdirs();
                                }
                            }
                            OutputStream out = new FileOutputStream(
                                    destinationDir + File.separator + item.getPath());

                            out.write(data);
                            out.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        hash[0] |= Arrays.hashCode(data);
                        return data.length; // Return amount of proceed data
                    }
                }); 
                if (result == ExtractOperationResult.OK) {
                    System.out.println(String.format("%9X | %s", hash[0], item.getPath()));
                } else {
                    System.err.println("Error extracting item: " + result);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (inArchive != null) {
            try {
                inArchive.close();
            } catch (SevenZipException e) {
                System.err.println("Error closing archive: " + e);
                e.printStackTrace();
            }
        }
        if (randomAccessFile != null) {
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                System.err.println("Error closing file: " + e);
                e.printStackTrace();
            }
        }
    }
}

As i debugged it. It is not entering the below block.

 result = item.extractSlow(new ISequentialOutStream() {//write() method})

Wanted to know How can i modify the code to make it work.

LathaPatil
  • 143
  • 1
  • 3
  • 9
  • What is happening when the file has no text ? I assume "no text" means it has a length of 0 byte. – Marged Oct 14 '15 at 12:27
  • Yes. No text means it has length of 0 byte and When there is no text Unzipping is not happening and error also not displayed. – LathaPatil Oct 14 '15 at 13:12

1 Answers1

2

Your code only creates a file if its payload is larger than 0 bytes. I believe that's because ISequentialOutStream write(byte[] data) - method only writes if the array is larger than 0 bytes. From the javadocs:

Write data byte array to the stream. If data.length > 0 this function must write at least 1 byte.

You should change the write() - methods code to allow empty file creation. Maybe this helps: Stackoverflow: decompress files with .7z extension in java

Community
  • 1
  • 1
Gernot
  • 1,094
  • 4
  • 25
  • 39
  • It is not entering the write method only when there is no text in the file. I feel it is not entering the below block only.. result = item.extractSlow(new ISequentialOutStream() { //write() method }) – LathaPatil Oct 14 '15 at 13:17
  • Thanks for the code link. But the code provided in the link works like my code only. It does not unzip if there is no data in file. – LathaPatil Oct 15 '15 at 05:38