SharArchive

Inheritance: java.lang.Object

All Implemented Interfaces: java.lang.AutoCloseable

public class SharArchive implements AutoCloseable

This class represents shar archive file.

Constructors

ConstructorDescription
SharArchive()Initializes a new instance of the SharArchive class.
SharArchive(String path)Initializes a new instance of the SharArchive class prepared for decompressing.

Methods

MethodDescription
close(){@inheritDoc}
createEntries(File directory)Adds to the archive all the files and directories recursively in the directory given.
createEntries(File directory, boolean includeRootDirectory)Adds to the archive all the files and directories recursively in the directory given.
createEntries(String sourceDirectory)Adds to the archive all the files and directories recursively in the directory given.
createEntries(String sourceDirectory, boolean includeRootDirectory)Adds to the archive all the files and directories recursively in the directory given.
createEntry(String name, File file)Create single entry within the archive.
createEntry(String name, File file, boolean includeRootDirectory)Create single entry within the archive.
createEntry(String name, InputStream source)Create single entry within the archive.
createEntry(String name, String sourcePath)Create single entry within the archive.
createEntry(String name, String sourcePath, boolean openImmediately)Create single entry within the archive.
deleteEntry(SharEntry entry)Removes the first occurrence of a specific entry from the entries list.
deleteEntry(int entryIndex)Removes the entry from the entries list by index.
getEntries()Gets entries of SharEntry type constituting the archive.
save(OutputStream output)Saves archive to the stream provided.
save(String destinationFileName)Saves archive to destination file provided.

SharArchive()

public SharArchive()

Initializes a new instance of the SharArchive class.

The following example shows how to compress a file.


     try (SharArchive archive = new SharArchive()) {
         archive.createEntry("first.bin", "data.bin");
         archive.save("archive.shar");
     }
 

SharArchive(String path)

public SharArchive(String path)

Initializes a new instance of the SharArchive class prepared for decompressing.

Parameters:

ParameterTypeDescription
pathjava.lang.Stringthe path to the source of the archive

close()

public void close()

createEntries(File directory)

public final SharArchive createEntries(File directory)

Adds to the archive all the files and directories recursively in the directory given.


     try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
         try (SharArchive archive = new SharArchive()) {
             archive.createEntries(new java.io.File("C:\\folder"), false);
             archive.save(sharFile);
         }
     } catch (IOException ex) {
     }
 

Parameters:

ParameterTypeDescription
directoryjava.io.Filethe directory to compress

Returns: SharArchive - Shar entry instance

createEntries(File directory, boolean includeRootDirectory)

public final SharArchive createEntries(File directory, boolean includeRootDirectory)

Adds to the archive all the files and directories recursively in the directory given.


     try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
         try (SharArchive archive = new SharArchive()) {
             archive.createEntries(new java.io.File("C:\\folder"), false);
             archive.save(sharFile);
         }
     } catch (IOException ex) {
     }
 

Parameters:

ParameterTypeDescription
directoryjava.io.Filethe directory to compress
includeRootDirectorybooleanindicates whether to include the root directory itself or not

Returns: SharArchive - Shar entry instance

createEntries(String sourceDirectory)

public final SharArchive createEntries(String sourceDirectory)

Adds to the archive all the files and directories recursively in the directory given.


     try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
         try (SharArchive archive = new SharArchive()) {
             archive.createEntries("C:\\folder", false);
             archive.save(sharFile);
         }
     } catch (IOException ex) {
     }
 

Parameters:

ParameterTypeDescription
sourceDirectoryjava.lang.Stringthe directory to compress

Returns: SharArchive - Shar entry instance

createEntries(String sourceDirectory, boolean includeRootDirectory)

public final SharArchive createEntries(String sourceDirectory, boolean includeRootDirectory)

Adds to the archive all the files and directories recursively in the directory given.


     try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
         try (SharArchive archive = new SharArchive()) {
             archive.createEntries("C:\\folder", false);
             archive.save(sharFile);
         }
     } catch (IOException ex) {
     }
 

Parameters:

ParameterTypeDescription
sourceDirectoryjava.lang.Stringthe directory to compress
includeRootDirectorybooleanindicates whether to include the root directory itself or not

Returns: SharArchive - Shar entry instance

createEntry(String name, File file)

public final SharEntry createEntry(String name, File file)

Create single entry within the archive.


     java.io.File file = new java.io.File("data.bin");
     try (SharArchive archive = new SharArchive()) {
         archive.createEntry("test.bin", file);
         archive.save("archive.shar");
     }
 

Parameters:

ParameterTypeDescription
namejava.lang.Stringthe name of the entry
filejava.io.Filethe metadata of file or folder to be compressed

Returns: SharEntry - Shar entry instance

createEntry(String name, File file, boolean includeRootDirectory)

public final SharEntry createEntry(String name, File file, boolean includeRootDirectory)

Create single entry within the archive.


     java.io.File file = new java.io.File("data.bin");
     try (SharArchive archive = new SharArchive()) {
         archive.createEntry("test.bin", file);
         archive.save("archive.shar");
     }
 

Parameters:

ParameterTypeDescription
namejava.lang.Stringthe name of the entry
filejava.io.Filethe metadata of file or folder to be compressed
includeRootDirectorybooleanindicates whether to include the root directory itself or not

Returns: SharEntry - Shar entry instance

createEntry(String name, InputStream source)

public final SharEntry createEntry(String name, InputStream source)

Create single entry within the archive.


     try (SharArchive archive = new SharArchive()) {
         archive.createEntry("data.bin", new FileInputStream("data.bin"));
         archive.save("archive.shar");
     } catch (IOException ex) {
     }
 

Parameters:

ParameterTypeDescription
namejava.lang.Stringthe name of the entry
sourcejava.io.InputStreamthe input stream for the entry

Returns: SharEntry - Shar entry instance

createEntry(String name, String sourcePath)

public final SharEntry createEntry(String name, String sourcePath)

Create single entry within the archive.


     try (SharArchive archive = new SharArchive()) {
         archive.createEntry("first.bin", "data.bin");
         archive.save("archive.shar");
     }
 

The entry name is solely set within name parameter. The file name provided in sourcePath parameter does not affect the entry name.

Parameters:

ParameterTypeDescription
namejava.lang.Stringthe name of the entry
sourcePathjava.lang.Stringthe path to the file to be compressed

Returns: SharEntry - Shar entry instance

createEntry(String name, String sourcePath, boolean openImmediately)

public final SharEntry createEntry(String name, String sourcePath, boolean openImmediately)

Create single entry within the archive.


     try (SharArchive archive = new SharArchive()) {
         archive.createEntry("first.bin", "data.bin");
         archive.save("archive.shar");
     }
 

The entry name is solely set within name parameter. The file name provided in sourcePath parameter does not affect the entry name.

If the file is opened immediately with openImmediately parameter it becomes blocked until archive is disposed.

Parameters:

ParameterTypeDescription
namejava.lang.Stringthe name of the entry
sourcePathjava.lang.Stringthe path to file to be compressed
openImmediatelybooleantrue if open the file immediately, otherwise open the file on archive saving

Returns: SharEntry - Shar entry instance

deleteEntry(SharEntry entry)

public final SharArchive deleteEntry(SharEntry entry)

Removes the first occurrence of a specific entry from the entries list.

Here is how you can remove all entries except the last one:


     try (SharArchive archive = new SharArchive("archive.shar")) {
         while (archive.getEntries().size() > 1)
             archive.deleteEntry(archive.getEntries().get(0));
         archive.save("outputSharFile.shar");
     }
 

Parameters:

ParameterTypeDescription
entrySharEntrythe entry to remove from the entries list

Returns: SharArchive - Shar entry instance

deleteEntry(int entryIndex)

public final SharArchive deleteEntry(int entryIndex)

Removes the entry from the entries list by index.


     try (SharArchive archive = new SharArchive("two_files.shar")) {
         archive.deleteEntry(0);
         archive.save("single_file.shar");
     }
 

Parameters:

ParameterTypeDescription
entryIndexintthe zero-based index of the entry to remove

Returns: SharArchive - the archive with the entry deleted

getEntries()

public final List<SharEntry> getEntries()

Gets entries of SharEntry type constituting the archive.

Returns: java.util.List<com.aspose.zip.SharEntry> - entries of SharEntry type constituting the archive

save(OutputStream output)

public final void save(OutputStream output)

Saves archive to the stream provided.


     try (FileOutputStream sharFile = new FileOutputStream("archive.shar")) {
         try (SharArchive archive = new SharArchive()) {
             archive.createEntry("entry1", "data.bin");
             archive.save(sharFile);
         }
     } catch (IOException ex) {
     }
 

Parameters:

ParameterTypeDescription
outputjava.io.OutputStreamthe destination stream

save(String destinationFileName)

public final void save(String destinationFileName)

Saves archive to destination file provided.


     try (SharArchive archive = new SharArchive()) {
         archive.createEntry("entry1", "data.bin");
         archive.save("archive.shar");
     }
 

Parameters:

ParameterTypeDescription
destinationFileNamejava.lang.Stringthe path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.

It is possible to save an archive to the same path as it was loaded from. However, this is not recommended because this approach uses copying to temporary file |