Skip to content

Commit

Permalink
reintroduced ZipFileSystem methods to be used for renaming operations
Browse files Browse the repository at this point in the history
  • Loading branch information
optyfr committed Apr 1, 2024
1 parent 000bd5a commit 05092ef
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 9 deletions.
119 changes: 119 additions & 0 deletions jrmcore/src/main/java/jrm/profile/fix/actions/AddEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.Map;

import jrm.aui.progress.ProgressHandler;
import jrm.compressors.Archive;
Expand Down Expand Up @@ -378,6 +382,121 @@ private boolean dir2Archive(final Archive dstarchive)
return false;
}

@Override
public boolean doAction(final Session session, final FileSystem dstfs, final ProgressHandler handler, int i, int max)
{
final var dstpath = dstfs.getPath(entity.getName());
handler.setProgress(null, null, null, progress(i, max, String.format(session.getMsgs().getString(ADD_ENTRY_ADDING), entity.getName()))); //$NON-NLS-1$
switch(entry.getParent().getType())
{
case DIR:
return dir2FS(dstpath);
case FAKE:
return fake2FS(dstpath);
case ZIP:
return zip2FS(dstpath);
default:
return default2FS(session, dstpath);
}
}

/**
* @param session
* @param dstpath
* @return
*/
private boolean default2FS(final Session session, final Path dstpath)
{
Path srcpath = null;
try(Archive srcarchive = new SevenZipArchive(session, entry.getParent().getFile()))
{
final File srcfile;
if((srcfile=srcarchive.extract(entry.getFile())) != null)
{
final var parentDstPath = dstpath.getParent();
if(parentDstPath != null)
Files.createDirectories(parentDstPath);
srcpath = srcfile.toPath();
Files.copy(srcpath, dstpath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return true;
}
}
catch (IOException e)
{
Log.err(String.format(ADD_FROM_S_AT_S_TO_S_AT_S_FAILED, entry.getParent().getRelFile(), srcpath, parent.container.getFile().getName(), dstpath), e);
}
return false;
}

/**
* @param dstpath
* @return
*/
private boolean zip2FS(final Path dstpath)
{
Path srcpath = null;
try(final var srcfs = FileSystems.newFileSystem(entry.getParent().getFile().toPath(), Collections.singletonMap("readOnly", true));)
{
final var parentDstPath = dstpath.getParent();
if(parentDstPath != null)
Files.createDirectories(parentDstPath);
srcpath = srcfs.getPath(entry.getFile());
Files.copy(srcpath, dstpath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return true;
}
catch (IOException e)
{
Log.err(String.format(ADD_FROM_S_AT_S_TO_S_AT_S_FAILED, entry.getParent().getRelFile(), srcpath, parent.container.getFile().getName(), dstpath), e);
}
return false;
}

/**
* @param dstpath
* @return
*/
private boolean fake2FS(final Path dstpath)
{
Path srcpath = null;
try
{
final var parentDstPath = dstpath.getParent();
if(parentDstPath != null)
Files.createDirectories(parentDstPath);
srcpath = entry.getParent().getFile().getParentFile().toPath().resolve(entry.getFile());
Files.copy(srcpath, dstpath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return true;
}
catch (IOException e)
{
Log.err(String.format(ADD_FROM_S_TO_S_AT_S_FAILED, srcpath, parent.container.getFile().getName(), dstpath), e);
}
return false;
}

/**
* @param dstpath
* @return
*/
private boolean dir2FS(final Path dstpath)
{
Path srcpath = null;
try
{
var parentDstPath = dstpath.getParent();
if(parentDstPath != null)
Files.createDirectories(parentDstPath);
srcpath = entry.getParent().getFile().toPath().resolve(entry.getFile());
Files.copy(srcpath, dstpath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return true;
}
catch (IOException e)
{
Log.err(String.format(ADD_FROM_S_TO_S_AT_S_FAILED, srcpath, parent.container.getFile().getName(), dstpath), e);
}
return false;
}

@Override
public String toString()
{
Expand Down
69 changes: 69 additions & 0 deletions jrmcore/src/main/java/jrm/profile/fix/actions/BackupEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import java.io.File;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import jrm.aui.progress.ProgressHandler;
import jrm.compressors.Archive;
Expand Down Expand Up @@ -139,4 +142,70 @@ else if(entry.getParent().getType() == Type.SEVENZIP)
}
return false;
}

@Override
public boolean doAction(final Session session, final FileSystem dstfs, final ProgressHandler handler, int i, int max)
{
final var dstPathCrc = dstfs.getPath(entry.getCrc()+'_'+entry.getSize());
final Path dstPath;
if(entry.getSha1()!=null)
dstPath = dstfs.getPath(entry.getSha1());
else if(entry.getMd5()!=null)
dstPath = dstfs.getPath(entry.getMd5());
else
dstPath = dstfs.getPath(entry.getCrc()+'_'+entry.getSize());
handler.setProgress(null, null, null, progress(i, max, String.format("Backup of %s", entry.getName()))); //$NON-NLS-1$
Path srcpath = null;
try
{
final var parent2 = dstPath.getParent();
if(parent2 != null)
Files.createDirectories(parent2);
if(!dstPath.equals(dstPathCrc) && Files.exists(dstPathCrc))
Files.delete(dstPathCrc);
if (Files.exists(dstPath))
return true;
if(entry.getParent().getType() == Type.DIR)
{
srcpath = entry.getParent().getFile().toPath().resolve(entry.getFile());
Files.copy(srcpath, dstPath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return true;
}
else if(entry.getParent().getType() == Type.FAKE)
{
srcpath = entry.getParent().getFile().getParentFile().toPath().resolve(entry.getFile());
Files.copy(srcpath, dstPath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return true;
}
else if(entry.getParent().getType() == Type.ZIP)
{
try(final var srcfs = FileSystems.newFileSystem(entry.getParent().getFile().toPath(), (ClassLoader)null);)
{
srcpath = srcfs.getPath(entry.getFile());
Files.copy(srcpath, dstPath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return true;
}
}
else if(entry.getParent().getType() == Type.SEVENZIP)
{
try(Archive srcarchive = new SevenZipArchive(session, entry.getParent().getFile()))
{
if(srcarchive.extract(entry.getFile()) != null)
{
srcpath = new File(srcarchive.getTempDir(), entry.getFile()).toPath();
Files.copy(srcpath, dstPath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return true;
}
}
}
}
catch(final Exception e)
{
Log.err(e.getMessage(),e);
Log.err("add from " + entry.getParent().getRelFile() + "@" + srcpath + " to " + parent.container.getFile().getName() + "@" + dstPath + " failed"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
return false;
}


}
23 changes: 23 additions & 0 deletions jrmcore/src/main/java/jrm/profile/fix/actions/ContainerAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package jrm.profile.fix.actions;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -226,5 +227,27 @@ protected boolean zosAction(final Session session, final ProgressHandler handler
}
return true;
}

/**
* @param session
* @param handler
* @param fs
* @return
*/
protected boolean fsAction(final Session session, final ProgressHandler handler, final FileSystem fs)
{
var i = 0;
for (final EntryAction action : entryActions)
{
i++;
if (!action.doAction(session, fs, handler, i, entryActions.size() ))
{
Log.err(()->String.format(ACTION_TO_S_AT_S_FAILED, container.getFile().getName(), action.entry.getRelFile()));
return false;
}
}
return true;
}


}
19 changes: 19 additions & 0 deletions jrmcore/src/main/java/jrm/profile/fix/actions/DeleteEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package jrm.profile.fix.actions;

import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -50,6 +51,24 @@ public DeleteEntry(final Entry entry)
super(entry);
}

@Override
public boolean doAction(final Session session, final FileSystem dstfs, final ProgressHandler handler, int i, int max)
{
Path path = null;
try
{
handler.setProgress(null, null, null, progress(i, max, String.format(session.getMsgs().getString(DELETE_ENTRY_DELETING), entry.getRelFile()))); //$NON-NLS-1$
path = dstfs.getPath(entry.getFile());
Files.deleteIfExists(path);
return true;
}
catch(final Exception e)
{
Log.err(String.format(DELETE_S_AT_S_FAILED, parent.container.getFile().getName(), path)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
return false;
}

@SuppressWarnings("exports")
@Override
public boolean doAction(Session session, ZipFile zipf, ZipParameters zipp, ProgressHandler handler, int i, int max)
Expand Down
24 changes: 24 additions & 0 deletions jrmcore/src/main/java/jrm/profile/fix/actions/DuplicateEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package jrm.profile.fix.actions;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import jrm.aui.progress.ProgressHandler;
import jrm.compressors.Archive;
Expand Down Expand Up @@ -55,6 +57,28 @@ public DuplicateEntry(final String newname, final Entry entry)
this.newname = newname;
}

@Override
public boolean doAction(final Session session, final FileSystem fs, final ProgressHandler handler, int i, int max)
{
final var dstpath = fs.getPath(newname);
try
{
handler.setProgress(null, null, null, progress(i, max, String.format(session.getMsgs().getString(DUPLICATE_ENTRY_DUPLICATING), entry.getRelFile(), newname))); //$NON-NLS-1$
final var srcpath = fs.getPath(entry.getFile());
final var parent2 = dstpath.getParent();
if(parent2 != null)
Files.createDirectories(parent2);
Files.copy(srcpath, dstpath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return true;
}
catch(final Exception e)
{
Log.err(e.getMessage(),e);
Log.err(String.format(DUPLICATE_S_AT_S_TO_S_AT_S_FAILED, parent.container.getFile().getName(), entry.getRelFile(), parent.container.getFile().getName(), newname));
}
return false;
}

@SuppressWarnings("exports")
@Override
public boolean doAction(Session session, ZipFile zipf, ZipParameters zipp, ProgressHandler handler, int i, int max)
Expand Down
11 changes: 11 additions & 0 deletions jrmcore/src/main/java/jrm/profile/fix/actions/EntryAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ protected EntryAction(final Entry entry)
@SuppressWarnings("exports")
public abstract boolean doAction(final Session session, final ZipFile zipf, final ZipParameters zipp, ProgressHandler handler, int i, int max);

/**
* do action on entry on a {@link FileSystem}
* @param session the current {@link Session}
* @param fs the {@link FileSystem} provided by {@link ContainerAction#doAction(Session, ProgressHandler)} in which we should apply entry action
* @param handler handler the {@link ProgressHandler} to show progression state
* @param i the progression level
* @param max the progression maximum
* @return true if successful, otherwise false
*/
public abstract boolean doAction(final Session session, FileSystem fs, ProgressHandler handler, int i, int max);

public long estimatedSize()
{
return ESTIMATED_SIZE;
Expand Down
Loading

0 comments on commit 05092ef

Please sign in to comment.